PHP致命错误:在Laravel4中播种期间调用未定义的方法setContainer()
我试图在用户和角色之间建立一种n:n关系。 迁移是正常的,但当我尝试在RoleUserTableSeeder上为db种子时,控制台会出现以下错误: PHP致命错误:在第57行的/var/www/laravel/vendor/laravel/framework/src/illighte/Database/Seeder.PHP中调用未定义的方法CreateForeignKeys::setContainer() {“error”:{“type”:“Symfony\Component\Debug\Exception\FatalErrorException”,“message”:“调用未定义的方法CreateForeignKeys::setContainer()”,“file”:“/var/www/laravel/vendor/laravel/framework/src/light/Database/Seeder.php”,“line”:57} 以下是我的迁移: 2014_10_27_153245_create_users_table.phpPHP致命错误:在Laravel4中播种期间调用未定义的方法setContainer(),php,laravel,orm,laravel-4,seeding,Php,Laravel,Orm,Laravel 4,Seeding,我试图在用户和角色之间建立一种n:n关系。 迁移是正常的,但当我尝试在RoleUserTableSeeder上为db种子时,控制台会出现以下错误: PHP致命错误:在第57行的/var/www/laravel/vendor/laravel/framework/src/illighte/Database/Seeder.PHP中调用未定义的方法CreateForeignKeys::setContainer() {“error”:{“type”:“Symfony\Component\Debug\Exc
<?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');
}
}
?>
<?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');
}
}
?>
<?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');
}
}
?>
有人能帮我纠正这个错误吗?
谢谢 可以添加Seeder.php吗?或者第57行指向哪个函数调用?感谢您的帮助,但我已使用委托解决了问题。
<?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');
});
}
}
?>
<?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',
));
}
}
?>
<?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'
));
}
}
?>
<?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')
));
}
}
?>
...
## Return the id of a role
public static function getId($mansion) {
return Role::where('role','=',$mansion)->pluck('id');
}
...