1

I have pivot table taggable, it's table for Many To Many Polymorphic Relations (Tag and post) in Laravel:

taggable_id - id of Post
tag_id - id of Tag
taggable type - location of post model (default value"App/Models/Posts/Post")
is_search_term - boolen (0 or 1)

How create seeder which for post with id 1 (taggable_id) set 0 tag id.

For post with id 2 set tag id 1.

For post with id 3 set tag id 1 and id 2.

Georg
  • 107
  • 1
  • 10

1 Answers1

1

Try this. Check the namespace of the Model if it matches your convention or not .Since Your 'taggable type' attribute has default type, excluded it from seeding through faker.

$factory->define(App/Models/Posts/Post::class, function (Faker\Generator $faker) {

return [
    'taggable_id' => function(){
        return factory('App/Models/Posts/Post')->create()->id;
    },
    'tag_id' => function(){
        return factory('App/Models/Tags/Tag')->create()->id;
    },
    'is_search_term' => $faker->boolean
]; });
sndsabin
  • 164
  • 1
  • 6
  • @Georg Could you please elaborate mate ? – sndsabin Aug 15 '17 at 09:39
  • I want that algorithm create fields like this: – Georg Aug 15 '17 at 10:04
  • taggable_id 1, tag_id 1 taggable_id 2, tag_id 1 taggable_id 2, tag_id 2 taggable_id 3, tag_id 1 – Georg Aug 15 '17 at 10:05
  • Do you mean if you have taggable_id = n, then tag_id = m(m is any number) but count of entry in taggable table with the tag_id for the same taggable_id(taggable_id = n) should be less than n-1 ? Or is it if the taggable_id = n then faker should create multiple records having tag_id in the range of 1 to n-1 and taggable_id = n ? – sndsabin Aug 15 '17 at 11:25