[英]Unable to seed db table with relationship in Laravel
In a laravel 5.8 application, I want to seed the users & products table. 在laravel 5.8应用程序中,我想为用户和产品表添加种子。 There is a relationship between the users & products like this
这样的用户和产品之间存在联系
User.php model (users can have one or more products) User.php模型(用户可以拥有一个或多个产品)
public function products()
{
return $this->hasMany(Product::class, 'user_id');
}
Product.php model (a product can belong to one or more users) Product.php模型(一种产品可以属于一个或多个用户)
public function users()
{
return $this->belongsToMany(User::class);
}
I am trying to use the UsersTableSeeder
below to seed both the users table & products table at the same time 我正在尝试使用下面的
UsersTableSeeder
来同时播种用户表和产品表
public function run()
{
factory(App\User::class, 3)->create()->each(function ($user) {
$user->products()->save(factory(App\Product::class, 3)->make());
});
}
and the 'DatabaseSeeder`looks like this 而“ DatabaseSeeder”看起来像这样
public function run()
{
$this->call(UsersTableSeeder::class);
}
When I run the command php artisan db:seed
, only the users table is seeded and I get this error 当我运行命令
php artisan db:seed
,仅php artisan db:seed
了users表,并且出现此错误
Symfony\\Component\\Debug\\Exception\\FatalThrowableError : Argument 1 passed to Illuminate\\Database\\Eloquent\\Relations\\HasOneOrMany::save() must be an instance of Illuminate\\Database\\Eloquent\\Model, instance of Illuminate\\Database\\Eloquent\\Collection given, called in C:\\Users\\Elomena\\Projects\\Clients\\Pramopro\\database\\seeds\\UsersTableSeeder.php on line 15
Symfony \\ Component \\ Debug \\ Exception \\ FatalThrowableError:传递给Illuminate \\ Database \\ Eloquent \\ Relations \\ HasOneOrMany :: save()的参数1必须是Illuminate \\ Database \\ Eloquent \\ Model的实例,Illuminate \\ Database \\ Eloquent \\ Collection的实例给定,在第15行的C:\\ Users \\ Elomena \\ Projects \\ Clients \\ Pramopro \\ database \\ seeds \\ UsersTableSeeder.php中调用
This is line 15 $user->products()->save(factory(App\\Product::class, 3)->make());
这是第15行
$user->products()->save(factory(App\\Product::class, 3)->make());
I really don't understand why I am getting this error as I have followed the exact thing from the https://laravel.com/docs/5.8/seeding#using-model-factories 我真的不明白为什么会收到这个错误,因为我已经遵循了https://laravel.com/docs/5.8/seeding#using-model-factories中的确切内容
Please how should seeding with relationships be done? 请如何播种关系?
This can be solve your problem: 这可以解决您的问题:
public function run()
{
factory(App\User::class, 3)->create()->each(function ($user) {
$user->products()->saveMany(factory(App\Product::class, 3)->create());
});
}
The error message suggests that you're using a collection instead of a model. 该错误消息表明您使用的是集合而不是模型。
The error is caused by this function, it returns a collection and not a model, because it's a hasMany
relationship. 该错误是由该函数引起的,它返回一个集合而不是一个模型,因为它是一个
hasMany
关系。
public function products()
{
return $this->hasMany(Product::class, 'user_id');
}
So, you should change your seeder to saveMany
instead of save
. 因此,您应该将播种器更改为
saveMany
而不是save
。
public function run()
{
factory(App\User::class, 3)->create()->each(function ($user) {
$user->products()->saveMany(factory(App\Product::class, 3)->create());
}); // ^ saveMany instead of save
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.