如何在 Laravel 中编写播种器
How write seeder in Laravel
我有 table 可标记,它是 table 用于多对多多态关系(标记和 post):
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)
如何创建一个每次都为关系创建相同记录的播种机?
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),
];
});
我有 table 可标记,它是 table 用于多对多多态关系(标记和 post):
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)
如何创建一个每次都为关系创建相同记录的播种机?
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),
];
});