2017-08-15 39 views
1

我有表可加标签写播种机,它的表多对多多态关系(标签和后):如何在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) 

如何创建,每个时间关系创建相同记录的播种机?

+0

这可能有助于尽管它laravel4 [Laravel 4:用种子关系工作(https://stackoverflow.com/questions/14666277/laravel-4-working-with-relationships-in-seeds) –

回答

2

如果Tag_id设置为主键,tag_id将由db自动创建,taggable_id可以从Tag模型获取值。

你可以尝试这样的事情;

$factory->define(App\Tag::class, function (Faker\Generator $faker) { 
    return [ 
     'taggable_id' => random_int(\DB::table('posts')->min('id'), \DB::table('posts')->max('id')), 
     'is_search_term'=>$faker->numberBetween(0,1), 
'taggable_type'=>$faker->sentence(2), 

    ]; 
}); 
+0

但是当我开始新播种时,这是与标签的新关系帖子。 – Georg

+0

你是什么意思? –

+0

我开始种子关系的所有时间必须相同(表中的字段必须相同,但不是随机的) – Georg