如何在播种机中附加功能?
How can I attach functions in seeder?
我要附上作者()
这是我在 UsersTableSeeder 中的代码
factory(User::class , 10)->create()->each(function ($user){
$user
->books()->create(factory(Book::class)->make()->toArray())
->categories()->attach([1,2,3])
->authors()->attach([1,2]);
});
但是我得到了 'calling function authors on null'
的错误
attach
没有 return 模型。你不能做所有的内联。尝试这样做:
factory(User::class , 10)->create()->each(function ($user) {
$book = factory(Book::class)->make();
$user->books()->save($book);
$book->categories()->attach([1,2,3]);
$book->authors()->attach([1,2])
});
我要附上作者()
这是我在 UsersTableSeeder 中的代码
factory(User::class , 10)->create()->each(function ($user){
$user
->books()->create(factory(Book::class)->make()->toArray())
->categories()->attach([1,2,3])
->authors()->attach([1,2]);
});
但是我得到了 'calling function authors on null'
的错误attach
没有 return 模型。你不能做所有的内联。尝试这样做:
factory(User::class , 10)->create()->each(function ($user) {
$book = factory(Book::class)->make();
$user->books()->save($book);
$book->categories()->attach([1,2,3]);
$book->authors()->attach([1,2])
});