Laravel - 播种多对多关系
Laravel - Seeding Many-to-Many Relationship
我有一个users
table和一个roles
table具有 多对多 关系。这两个 table 连接到一个名为 role_user
.
的结点 table
This is a model of the tables and its connections.
以下是我的 Laravel 项目中的模型:
用户
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
角色
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users()
{
return $this->belongsToMany('App\User');
}
}
下面是 Laravel 项目中的 Factory 文件:
$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),
];
});
下面是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());
});
}
这应该填充 users
table 和 roles
table 但我该如何填充 role_user
table? (我没有结点的模型文件 table。)
我在这方面还很陌生,如有任何帮助,我们将不胜感激。谢谢。
您可以在多对多关系上使用 attach()
or sync()
method。
您可以通过多种方式解决这个问题。这是其中之一:
// 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()
);
});
对于播种机,您可以使用这样的东西:
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
]);
}
但通常你需要通过 https://laravel.com/docs/5.4/eloquent-relationships#has-many-through
来定义像 has many 这样的关系
那么您将可以使用:
$user->roles()->save($role);
另一种方法是使用 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);
});
}
一个更简洁的方法可以是:在为 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));
});
然后在 Seeds 中首先创建角色,然后是用户
public function run()
{
factory(App\Role::class, 20)->create();
factory(App\User::class, 50)->create();
}
现在您有 50 个用户,每个用户都附加了一个角色。
最好使用这种结构:
App\Role::factory()->count(20)->create();
// Populate users
App\User::factory()->count(50)->create();
// Getting all roles and saving them to variable is not too good idea.
// Instead, get count of rows.
$rolesCount = App\Role::count();
// Populate the pivot table
App\User::all()->each(function ($user) use ($rolesCount) {
$user->roles()->attach(
App\Role::all()->random(rand(1, $rolesCount))->pluck('id')->toArray()
);
});
我有一个users
table和一个roles
table具有 多对多 关系。这两个 table 连接到一个名为 role_user
.
This is a model of the tables and its connections.
以下是我的 Laravel 项目中的模型:
用户
namespace App;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
public function roles()
{
return $this->belongsToMany('App\Role');
}
}
角色
namespace App;
use Illuminate\Database\Eloquent\Model;
class Role extends Model
{
public function users()
{
return $this->belongsToMany('App\User');
}
}
下面是 Laravel 项目中的 Factory 文件:
$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),
];
});
下面是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());
});
}
这应该填充 users
table 和 roles
table 但我该如何填充 role_user
table? (我没有结点的模型文件 table。)
我在这方面还很陌生,如有任何帮助,我们将不胜感激。谢谢。
您可以在多对多关系上使用 attach()
or sync()
method。
您可以通过多种方式解决这个问题。这是其中之一:
// 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()
);
});
对于播种机,您可以使用这样的东西:
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
]);
}
但通常你需要通过 https://laravel.com/docs/5.4/eloquent-relationships#has-many-through
来定义像 has many 这样的关系那么您将可以使用:
$user->roles()->save($role);
另一种方法是使用 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);
});
}
一个更简洁的方法可以是:在为 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));
});
然后在 Seeds 中首先创建角色,然后是用户
public function run()
{
factory(App\Role::class, 20)->create();
factory(App\User::class, 50)->create();
}
现在您有 50 个用户,每个用户都附加了一个角色。
最好使用这种结构:
App\Role::factory()->count(20)->create();
// Populate users
App\User::factory()->count(50)->create();
// Getting all roles and saving them to variable is not too good idea.
// Instead, get count of rows.
$rolesCount = App\Role::count();
// Populate the pivot table
App\User::all()->each(function ($user) use ($rolesCount) {
$user->roles()->attach(
App\Role::all()->random(rand(1, $rolesCount))->pluck('id')->toArray()
);
});