Laravel Api - 如何放置喜欢和不喜欢

我正在尝试在我的 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”,如果返回的消息是“不喜欢”。


慕森卡
浏览 139回答 1
1回答

慕田峪9158850

这是一个应该与您一起使用的示例。<?php// Get the post from the database$post = \App\Post::find($request->id);// Find the SocialPost record if it exists,// if it doesn't create a new one with no likes$socialPost = SocialPost::firstOrCreate(&nbsp; &nbsp; ['user_id' => $request->user_id, 'post_id' => $post->id],&nbsp; &nbsp; ['user_id' => $request->user_id, 'post_id' => $post->id, 'likes' => 0, 'view' => 0, 'creator' => 1],);// Declare an empty variable that will determine if// the post is being "liked" or "disliked"$postAction = '';// Determine if the post has likes or notif ($socialPost->likes > 0){&nbsp; &nbsp; // The post has at least 1 like&nbsp; &nbsp; $postAction = 'dislike';&nbsp; &nbsp; // Decrement the likes by 1&nbsp; &nbsp; $socialPost->decrement('likes', 1);}else{&nbsp; &nbsp; // The post has 0 likes&nbsp; &nbsp; $postAction = 'like';&nbsp; &nbsp; // Increment the likes by 1&nbsp; &nbsp; $socialPost->increment('likes', 1);}// Determine if this was a new SocialPost recordif ($socialPost->wasRecentlyCreated){&nbsp; &nbsp; // Override the post action as "New table"&nbsp; &nbsp; $postAction = 'New table';}// Return the goodsreturn json_encode(['status' => true, 'msg' => $postAction]);解释您可以确定 SocialPost 记录是否存在,或创建一个新记录 - 使用此处firstOrCreate文档中引用的全部在一行中。第一个数组参数接受您要查找的内容,第二个数组参数接受如果第一个参数没有出现则应创建的内容// Find the SocialPost record if it exists,// if it doesn't create a new one with no likes$socialPost = SocialPost::firstOrCreate(&nbsp; &nbsp; ['user_id' => $request->user_id, 'post_id' => $post->id],&nbsp; &nbsp; ['user_id' => $request->user_id, 'post_id' => $post->id, 'likes' => 0, 'view' => 0, 'creator' => 1],);然后,您可以通过使用increment或decrement在此处的文档中引用来清理加减点赞。// Determine if the post has likes or notif ($socialPost->likes > 0){&nbsp; &nbsp; // Decrement the likes by 1&nbsp; &nbsp; $socialPost->decrement('likes', 1);}else{&nbsp; &nbsp; // Increment the likes by 1&nbsp; &nbsp; $socialPost->increment('likes', 1);}
打开App,查看更多内容
随时随地看视频慕课网APP