我正在尝试在我的 Laravel 项目中创建一个喜欢和不喜欢的 API 请求:
Route::post('like', 'Api\ApiController@like');
ApiController 中的函数如下所示:
$post = \App\Post::find($request->id);
$social = SocialPost::where([['user_id', '=', ($request->user_id)],['post_id', '=', ($request->id)]])->first();
$unsocial = SocialPost::where([['user_id', '=', ($request->user_id)],['post_id', '=', ($request->id)],['like', '=', 0]])->first();
if ($social) {
if ($unsocial) {
$social->update(['like' => 1]);
return json_encode(array('status' => true,'msg'=>'like'));
}
else {
$social->update(['like' => 0]);
return json_encode(array('status' => true,'msg'=>'dislike'));
}
}
else {
$join = new \App\SocialPost;
$join->user_id = $request->user_id;
$join->post_id = $post->id;
$join->like = 1;
$join->view = 0;
$join->creator = 1;
$join->save();
return json_encode(array('status' => true,'msg'=>'New table'));
}
问题是第一个 If 语句有效,但第二个无效。如果该行已经存在,他总是为“喜欢”加上“1”,如果返回的消息是“不喜欢”。
慕田峪9158850