我试图在用户和角色之间建立n:n关系。 迁移是可以的,但是当我尝试在RoleUserTableSeeder上播种数据库时,控制台会给我这个错误:
PHP致命错误:在第57行的/var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Seeder.php中调用未定义的方法CreateForeignKeys :: setContainer() {"错误" {"类型":" Symfony的\元器件\调试\异常\ FatalErrorException""消息":"调用未定义的方法CreateForeignKeys :: setContainer()"," file":" /var/www/laravel/vendor/laravel/framework/src/Illuminate/Database/Seeder.php& #34;"线" 57}}
这些是我的迁移:
2014_10_27_153245_create_users_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
public function up()
{
Schema::create('users', function($t) {
$t->string('name', 50);
$t->string('surname', 50);
$t->string('email', 100);
$t->string('username', 20);
$t->string('password', 60);
$t->string('remember_token', 100);
$t->timestamps();
$t->primary('username');
});
}
public function down()
{
Schema::drop('users');
}
}
?>
2014_10_29_162948_create_roles_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRolesTable extends Migration {
public function up()
{
Schema::create('roles', function($t) {
$t->increments('id');
$t->string('role',50)->unique();
$t->timestamps();
});
}
public function down()
{
Schema::drop('roles');
}
}
?>
2014_10_29_163350_create_role_user_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateRoleUserTable extends Migration {
public function up()
{
Schema::create('role_user', function($t) {
$t->increments('id');
$t->integer('role_id')->unsigned();
$t->string('user_username');
});
}
public function down()
{
Schema::drop('role_user');
}
}
?>
2014_10_29_163723_create_foreign_keys.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateForeignKeys extends Migration {
public function up()
{
Schema::table('role_user', function($t) {
$t->foreign('role_id')->references('id')->on('roles')
->onUpdate('cascade');
});
Schema::table('role_user', function($t) {
$t->foreign('user_username')->references('username')->on('users')
->onUpdate('cascade');
});
}
public function down()
{
Schema::table('role_user', function($t) {
$t->dropForeign('role_user_role_id_foreign');
});
Schema::table('role_user', function($t) {
$t->dropForeign('role_user_user_username_foreign');
});
}
}
?>
......这些是我的播种机:
UserTableSeeder.php
<?php
## Seeder for the user table to create the admin user
class UserTableSeeder extends Seeder
{
public function run()
{
DB::table('users')->delete();
DB::table('users')->insert(array(
'name' => 'xxxx',
'surname' => 'xxxx',
'email' => 'xxxx@yyyy.com',
'password' => Hash::make('admin'),
'username' => 'admin',
));
}
}
?>
RoleTableSeeder.php
<?php
## Seeder for the role table to create the admin user
class RoleTableSeeder extends Seeder
{
public function run()
{
DB::table('roles')->delete();
DB::table('roles')->insert(array(
'role' => 'admin'
));
}
}
?>
RoleUserTableSeeder.php
<?php
## Seeder for the role_user table to create the admin user
class RoleUserTableSeeder extends Seeder
{
public function run()
{
DB::table('role_user')->delete();
DB::table('role_user')->insert(array(
'user_username' => 'admin',
'role_id' => Role::getId('admin')
));
}
}
?>
这是我在角色模型中的getId函数:
Role.php
...
## Return the id of a role
public static function getId($mansion) {
return Role::where('role','=',$mansion)->pluck('id');
}
...
有人可以帮我解决这个错误吗? 谢谢!