【问题标题】:Laravel Api - how to put like and dislikeLaravel Api - 如何放置喜欢和不喜欢
【发布时间】:2019-10-06 00:03:50
【问题描述】:

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

【问题讨论】:

  • 旁注: 在将变量添加到数组时,没有理由将它们放在括号中。它只会让阅读变得更加困难。

标签: php laravel api


【解决方案1】:

这是一个适合你的例子。

<?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 记录是否存在,或者创建一个新记录 - 使用文档 here 中引用的 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],
);

然后您可以使用文档here 中引用的incrementdecrement 来清理加减赞。

// 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);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-04-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    相关资源
    最近更新 更多