0

Hy guys!

I would just like to find out if there is an easy way to avoid duplication's when seeding a pivot table.

I have these two tables:

category

post

and a pivot table:

category_post

I can seed them, and it works, my relations are working two.

Everything works, except the fact that i have posts that belong to multiple categories.

So i want a post to belong to only one categore, categories of course have more posts.

This is how my category_post_seed file looks like:

class CategoryPostTableSeeder extends Seeder {

    public function run()
    {
        $faker = Faker::create();

        $categoryIds = Category::lists('id');
        $postIds = Post::lists('id');

        foreach(range(1, 50) as $index)
        {
            DB::table('category_post')->insert([
               'category_id' => $faker->randomElement($categoryIds),
               'post_id' => $faker->randomElement($postIds)
            ]);
        }
    }

}

Thanks for any help.

3
  • Why would you want that if you have many-to-many relationship? Commented Jun 30, 2014 at 21:02
  • @deczo i don't think i want/have many-to-man relationship, but i may be wrong. Could you elaborate? Right now i want one post to belong to only one category, but a category has a lot of posts. Is that many-to-man? Commented Jun 30, 2014 at 21:12
  • 1
    Many-to-many is a relation that you define with pivot table usualy, so you can link many posts to many categories. Otherwise you need one-to-many relation, which is, in the context of Eloquent, Category has many Post, and Post belongs to Category. Then there is a foreign key on the posts table that links to the primary key of the categories table, thus you won't be able to make one Post belong to many Category, which you have now. Commented Jul 1, 2014 at 11:04

1 Answer 1

3

You can use sync() which will remove all the rows from the pivot table for a certain post/category and then add a row for each item in the array. In this case, you'd only give it one item.

$category = Category::find(1);
$category->posts()->sync(array($somePostID));

    $faker = Faker::create();

    $categoryIds = Category::lists('id');
    $postIds = Post::lists('id');

    foreach(range(1, 50) as $index)
    {
        $category = Category::find($faker->randomElement($categoryIds));
        $category->posts()->sync(array($faker->randomElement($postIDs)));
    }
Sign up to request clarification or add additional context in comments.

5 Comments

i updated my question with my seed file, how can i accomplish what you said?
i tried it out, but the result was that: i have 30 posts, in my posts table, which is ok, i have 7 categories in my categories table, which is still ok, but i have only 7 rows in my category post pivot table, which is not ok. Was that suppose to happen?
Because you only have 7 categories. If you wanted 1 post per category, obviously all you will have in your pivot table is 7 items. Is this not what you wanted?
Thanks to @deczo i found out that my problem was that my relations are wrong i have one to many relation and not many to many, but to be honest your answer is a good solution for the question i posted, so thank you to.
Okay, that makes more sense, you had me pretty confused.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.