我目前使用Laravel 5.8,我的数据库有一个users
表和一个roles
表,它们之间存在多对多关系。我的意图是使用php artisan migrate:fresh --seed
一次性植入整个数据库,但是数据透视表role_user
始终为空,这表明附加/同步不正确。我打算实现更多对多的关系,因此我想彻底了解使用播种机和工厂来播种这些关系。
如何播种多对多关系并快速检查这些关系是否正常工作?
用户模型:
class User extends Authenticatable
{
//Default variables generated by Laravel
public function roles()
{
return $this->belongsToMany('App\Role', 'role_user')->withTimestamps();
}
}
角色模型:
class Role extends Model
{
public function users()
{
return $this->belongsToMany('App\User', 'role_user')->withTimestamps();
}
}
表role_user迁移:
public function up()
{
Schema::create('role_user', function (Blueprint $table) {
$table->bigIncrements('id');
$table->unsignedBigInteger('role_id');
$table->unsignedBigInteger('user_id');
$table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
$table->foreign('role_id')->references('id')->on('roles')->onDelete('cascade');
$table->timestamps();
}
播种者:
public function run()
{
$role_user = new Role();
$role_user->role = 'user';
$role_user->save();
$role_admin = new Role();
$role_admin->role = 'admin';
$role_admin->save();
}
用户种子:
public function run()
{
$faker = Faker\Factory::create();
$role_user = Role::where('role','=','user')->first();
$role_admin = Role::where('role','=','admin')->first();
//Generic user account for testing
$test_user = new User();
$test_user->username = 'user';
$test_user->email = 'user@example.com';
$test_user->email_verified_at = now();
$test_user->password = 'user';
$test_user->remember_token = Str::random(10);
$test_user->save();
$test_user->roles()->attach($role_user);
//Generic admin account for testing
$test_admin = new User();
$test_admin->username = 'admin';
$test_admin->email = 'admin@example.com';
$test_admin->email_verified_at = now();
$test_admin->password = 'admin';
$test_admin->remember_token = Str::random(10);
$test_admin->save();
$test_admin->roles()->attach($role_admin);
}
答案 0 :(得分:0)
如果您想通过工厂使用,
在工厂文件夹中创建UserFactory.php
<?php
use App\User;
use Illuminate\Support\Str;
use Faker\Generator as Faker;
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
$factory->define(App\User::class, function (Faker $faker) {
return [
'username' => $faker->name ,
'email' => $faker->unique()->safeEmail ,
'email_verified_at ' => Str::random(10),
'remember_token' => now(),
'password' => Hash::make('123456')
];
});
然后创建RoleUsersFactory.php
<?php
use App\User,App\Role,App\RoleUser;
use Illuminate\Support\Str;
use Faker\Generator as Faker;
/*
|--------------------------------------------------------------------------
| Model Factories
|--------------------------------------------------------------------------
|
| This directory should contain each of the model factory definitions for
| your application. Factories provide a convenient way to generate new
| model instances for testing / seeding your application's database.
|
*/
$factory->define(App\RoleUser::class, function (Faker $faker) {
return [
'role_id' => Role::all()->random()->id,
'user_id' => User::all()->random()->id,
];
});
然后在您的DatabaseSeeder.php中 添加此
factory(App\User::class, 50)->create()->each(function($u) {
$u->userroles()->save(factory(App\RoleUser::class)->make());
});
您还需要创建RoleUser模型,并且在User模型中,您设置的错误关系role_user
在您的角色表中不存在。
我希望这对您有帮助