Laravel - 播种多对多关系
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45269146/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel - Seeding Many-to-Many Relationship
提问by Bilal Khawar
I have a users
table and a roles
table that has a many-to-manyrelationship. These two tables are connected to a junction table called role_user
.
我有一个users
表和一个roles
具有多对多关系的表。这两个表连接到一个名为 的连接表role_user
。
This is a model of the tables and its connections.
Below are the Models in my Laravel project:
以下是我的 Laravel 项目中的模型:
User
用户
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
Role
角色
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users()
{
return $this->belongsToMany('App\User');
}
}
Below is the Factory file in the Laravel project:
下面是 Laravel 项目中的工厂文件:
$factory->define(App\User::class, function (Faker\Generator $faker) {
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'password' => $password ?: $password = bcrypt('secret'),
];
});
$factory->define(App\Role::class, function (Faker\Generator $faker) {
return [
'role' => $faker->realText($maxNbChars = 2),
'description' => $faker->realText($maxNbChars = 20),
];
});
Below is the Seed file in the Laravel project:
下面是 Laravel 项目中的种子文件:
public function run()
{
factory(App\User::class, 50)->create()->each(function ($u) {
$u->roles()->save(factory(App\Role::class)->make());
});
factory(App\Role::class, 20)->create()->each(function ($u) {
$u->users()->save(factory(App\User::class)->make());
});
}
This should populate the users
table and the roles
table but how do I go about populating the role_user
table? (I don't have a Model file for the junction table.)
这应该填充users
表格和roles
表格,但我该如何填充role_user
表格?(我没有连接表的模型文件。)
I'm very new at this so any help would be appreciated. Thanks.
我对此很陌生,因此任何帮助将不胜感激。谢谢。
回答by peterm
You can use attach()
or sync()
methodon a many-to-many relationship.
您可以在多对多关系上使用attach()
或sync()
方法。
There are multiple ways you can approach this. Here one of them:
有多种方法可以解决这个问题。这是其中之一:
// Populate roles
factory(App\Role::class, 20)->create();
// Populate users
factory(App\User::class, 50)->create();
// Get all the roles attaching up to 3 random roles to each user
$roles = App\Role::all();
// Populate the pivot table
App\User::all()->each(function ($user) use ($roles) {
$user->roles()->attach(
$roles->random(rand(1, 3))->pluck('id')->toArray()
);
});
回答by user1853220
Another way is to use saveMany() function
另一种方法是使用 saveMany() 函数
public function run()
{
factory(App\User::class,3)->create();
$roles = factory(App\Role::class,3)->create();
App\User::All()->each(function ($user) use ($roles){
$user->roles()->saveMany($roles);
});
}
回答by Oleg
Just for a seeder you can use something like this:
仅对于播种机,您可以使用以下内容:
for ($i = 0; $i < 50; $i++) {
$user = factory(App\User::class)->create();
$role = factory(App\Role::class)->create();
DB::table('role_user')->insert([
'user_id' => $user->id,
'role_id' => $role->id
]);
}
But normally you need to define relation like has many through https://laravel.com/docs/5.4/eloquent-relationships#has-many-through
但通常你需要通过https://laravel.com/docs/5.4/eloquent-relationships#has-many-through定义像 has many 这样的关系
Then you will be able to use:
然后你将能够使用:
$user->roles()->save($role);
回答by Razvan Theodor
A much cleaner method can be: after you define the factory for App\User and App\Roles you can call the afterCreatingmethod like this:
一个更简洁的方法可以是:在为 App\User 和 App\Roles 定义工厂之后,您可以像这样调用afterCreating方法:
$factory->define(App\User::class, function ...);
$factory->define(App\Role::class, function ...);
$factory->afterCreating(App\User::class, function ($row, $faker) {
$row->roles()->attach(rand(1,20));
});
Then in Seeds you first create the roles, then the users
然后在 Seeds 中,您首先创建角色,然后创建用户
public function run()
{
factory(App\Role::class, 20)->create();
factory(App\User::class, 50)->create();
}
Now you have 50 users each of them with one role attached.
现在您有 50 个用户,每个用户都附加了一个角色。