为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

PHP
慕森卡 2021-06-18 14:05:25
我正在尝试在我的 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”,如果返回的消息是“不喜欢”。
查看完整描述

1 回答

?
慕田峪9158850

TA贡献1794条经验 获得超7个赞

这是一个应该与您一起使用的示例。


<?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(

    ['user_id' => $request->user_id, 'post_id' => $post->id],

    ['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 not

if ($socialPost->likes > 0)

{

    // The post has at least 1 like

    $postAction = 'dislike';


    // Decrement the likes by 1

    $socialPost->decrement('likes', 1);

}

else

{

    // The post has 0 likes

    $postAction = 'like';


    // Increment the likes by 1

    $socialPost->increment('likes', 1);

}


// Determine if this was a new SocialPost record

if ($socialPost->wasRecentlyCreated)

{

    // Override the post action as "New table"

    $postAction = 'New table';

}


// Return the goods

return 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(

    ['user_id' => $request->user_id, 'post_id' => $post->id],

    ['user_id' => $request->user_id, 'post_id' => $post->id, 'likes' => 0, 'view' => 0, 'creator' => 1],

);

然后,您可以通过使用increment或decrement在此处的文档中引用来清理加减点赞。


// Determine if the post has likes or not

if ($socialPost->likes > 0)

{

    // Decrement the likes by 1

    $socialPost->decrement('likes', 1);

}

else

{

    // Increment the likes by 1

    $socialPost->increment('likes', 1);

}


查看完整回答
反对 回复 2021-06-25
  • 1 回答
  • 0 关注
  • 138 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信