【发布时间】:2019-03-16 11:18:14
【问题描述】:
我想更新帖子的隐私
我的控制器
public function changePostsPrivacy(Request $request){
$userId = $request->user()->id;
$postid = $request->get('id');
//dd($postid);
$privacy = $request->get('privacy');//dd($privacy);
$user = User::where(['id' => $userId, 'hide' => 0])->first();
if($user && $postid && in_array($privacy, [1,0])){
DB::table('posts')->update(['creator_id' => $userId, 'id' => $postid],[
'privacy' => $privacy,
]);
}
}
路线:
Route::group(['middleware'=>['auth:api', \App\Http\Middleware\OnlyRegisteredUsers::class]], function(){
/**
* Group for registered users only APIs
*/
Route::post('changePostsPrivacy','UserController@changePostsPrivacy');
});
迁移
public function up()
{
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('title');
$table->longText('content');
$table->string('short_description');
$table->unsignedInteger('media_id')->nullable();
$table->foreign('media_id')->references('id')->on('medias');
$table->unsignedInteger('creator_id');
$table->foreign('creator_id')->references('id')->on('users');
$table->boolean('hide')->default(0);
$table->timestamps();
});
}
此迁移中添加了新列
public function up()
{
Schema::table('posts', function (Blueprint $table) {
$table->integer('privacy')->after('creator_id');
});
}
当我想为任何帖子添加隐私时,它会给我一个错误
“消息”:“SQLSTATE [23000]:完整性约束违规:1451 无法删除或更新父行:外键约束失败 (
webdb.comments, 约束comments_post_id_foreign外键 (post_id) 参考posts(id)) (SQL: 更新posts设置creator_id= 17,id= 48)",
【问题讨论】:
标签: database laravel phpmyadmin postman database-migration