Laravel 5 插入带有外键的行
Laravel 5 inserting row with a foreign key
我有两个 tables 用户和帖子。
这是我的用户 table 迁移文件:
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->string('password_temp',60);
$table->integer('active');
$table->string('code',60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
这是我的 posts table 迁移文件
public function up()
{
Schema::create('posts', function(Blueprint $table){
$table->increments('id');
$table->string('title');
$table->text('body');
$table->integer('user_id')->unsigned();
$table->string('slug');
$table->timestamps();
});
Schema::table('posts',function(Blueprint $table){
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
}
AdminPostsController 扩展控制器{
public 函数存储(请求 $request)
{
$validator = Validator::make($request->all(),Post::$rules);
if($validator->passes()){
$post = new Post();
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->user_id = $request->get('id');
$post->slug = Slug::generateSlug($request->get('title'));
$post->save();
return Redirect::route('admin.posts.index');
}
else{
return Redirect::route('admin.posts.create')->withErrors($validator)->withInput();
}
}
}
每次我插入一个新的post,我总是看到以下错误
"QueryException in Connection.php line 614:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ('blog'.'posts', CONSTRAINT 'posts_user_id_foreign' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE ON UPDATE CASCADE)
"
我想知道我做错了什么。
此代码创建了一个约束,因此您的 post 必须由有效的用户 ID 引用。 user_id
字段必须包含用户 table id 字段上的现有键。
$table->foreign('user_id')
->references('id')
->on('users')
在保存新 post 之前尝试关联用户。
$post = new Post();
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->user()->associate($user);
$post->save();
假设您在 $user
var 上加载了一个有效的用户模型,并且您已经在模型上设置了用户和帖子之间的关系。
我有两个 tables 用户和帖子。 这是我的用户 table 迁移文件:
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('name');
$table->string('email')->unique();
$table->string('password', 60);
$table->string('password_temp',60);
$table->integer('active');
$table->string('code',60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
这是我的 posts table 迁移文件
public function up()
{
Schema::create('posts', function(Blueprint $table){
$table->increments('id');
$table->string('title');
$table->text('body');
$table->integer('user_id')->unsigned();
$table->string('slug');
$table->timestamps();
});
Schema::table('posts',function(Blueprint $table){
$table->foreign('user_id')
->references('id')
->on('users')
->onDelete('cascade')
->onUpdate('cascade');
});
}
AdminPostsController 扩展控制器{ public 函数存储(请求 $request) {
$validator = Validator::make($request->all(),Post::$rules);
if($validator->passes()){
$post = new Post();
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->user_id = $request->get('id');
$post->slug = Slug::generateSlug($request->get('title'));
$post->save();
return Redirect::route('admin.posts.index');
}
else{
return Redirect::route('admin.posts.create')->withErrors($validator)->withInput();
}
}
}
每次我插入一个新的post,我总是看到以下错误
"QueryException in Connection.php line 614:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails ('blog'.'posts', CONSTRAINT 'posts_user_id_foreign' FOREIGN KEY ('user_id') REFERENCES 'users' ('id') ON DELETE CASCADE ON UPDATE CASCADE)
"
我想知道我做错了什么。
此代码创建了一个约束,因此您的 post 必须由有效的用户 ID 引用。 user_id
字段必须包含用户 table id 字段上的现有键。
$table->foreign('user_id')
->references('id')
->on('users')
在保存新 post 之前尝试关联用户。
$post = new Post();
$post->title = $request->get('title');
$post->body = $request->get('body');
$post->user()->associate($user);
$post->save();
假设您在 $user
var 上加载了一个有效的用户模型,并且您已经在模型上设置了用户和帖子之间的关系。