【问题标题】:Upvote or downvote to the users posts赞成或反对用户的帖子
【发布时间】:2019-03-10 21:17:39
【问题描述】:

我想赞成或反对其他用户发布的帖子。用户将 上传一个帖子,我

投票表迁移

public function up()
{
    Schema::create('votes', function (Blueprint $table) {
        $table->increments('id');
        $table->unsignedInteger('user_id');
        $table->foreign('user_id')->references('id')->on('users');
        $table->unsignedInteger('post_id');
        $table->foreign('post_id')->references('id')->on('posts');
        $table->integer('vote');
        $table->boolean('hide')->default(0);
        $table->timestamps();
    });
}

路由文件

api.php

Route::group(['middleware'=>[
                'auth:api', \App\Http\Middleware\OnlyRegisteredUsers::class]
            ], function(){
    /**
     * Group for registered users only APIs
     */
    Route::post('saveMedia','UserController@saveMedia');
    Route::post('voteToPost','UserController@voteToPost');
});

控制器

public function voteToPost(Request $request)
{
    $userid = $request->user()->id;
    $postid = $request->get('post_id');
    $vote = $request->get('vote');
    $votecount = DB::table('votes')->where('user_id', $userid)->where('post_id', $postid)->wherea('hide', 0)->count();
    if($votecount == 0){
        DB::table('votes')
            ->leftJoin('users', 'users.id', '=', 'votes.user_id')
            ->leftJoin('posts', 'posts.id', '=', 'votes.post_id')
            ->where(['votes.hide' => 0, 'votes.user_id' =>$userid, 'votes.post_id' =>$postid])
            ->updateOrInsert([
                'user_id' => $userid,
                'post_id' => $postid,
                'vote' => $vote,
                'hide' => 0,
                'created_at' => Carbon::now(),
                'updated_at' => Carbon::now()
            ]);
    }
}

我收到以下错误 “消息”:“SQLSTATE [23000]:违反完整性约束:1052列'隐藏'在where子句中不明确(SQL:select存在(select * from votes left join users on users.id = votes.user_id left join posts on posts.id = votes.post_id where (votes.hide = 0 and votes@.@.@. votes.post_id = 6) 和 (user_id = 3 和 post_id = 6 和 vote = 1 和 hide = 0 和 created_at = 2018-10-05 11:44:38和updated_at = 2018-10-05 11:44:38)) 作为exists)",

【问题讨论】:

  • 您的问题是什么?你有什么错误?
  • 问题不清楚。您必须指定您的问题是什么。你想做什么,你遇到了什么问题?否则其他人会否决您的问题
  • 为什么会有这么复杂的代码?您只需要为特定的post 增加vote 列,不是吗?
  • @TharakaDilshan 如果我想对任何帖子投反对票或删除投赞成票怎么办
  • @TharakaDilshan 我只是在寻找这个。只需检查答案。

标签: database laravel oauth-2.0 phpmyadmin postman


【解决方案1】:
public function voteToPost(Request $request){
        $userid = $request->user()->id;
        $postid = $request->get('post_id');
        $vote = $request->get('vote');
        $user = User::where(['id'=>$userid, 'hide'=>0])->first();
        $post = DB::table('posts')->where(['id'=>$postid, 'hide'=>0])->first();
        if($user && $post && in_array($vote, [0,1,-1])){
                DB::table('votes')
                    ->updateOrInsert(['user_id'=>$userid, 'post_id'=>$postid],[
                        'user_id' => $userid,
                        'post_id' => $postid,
                        'vote' => $vote,
                        'hide' => 0,
                        'created_at' => Carbon::now(),
                        'updated_at' => Carbon::now()
                    ]);
                return ['message'=>'ok'];
        }
        return abort(403, 'Invalid request');
    }

【讨论】:

  • 我在这里看到一个问题,如果投票已经存在,这会覆盖投票数量,而不是增加或减少它。祝你好运
  • @TharakaDilshan 在这种情况下,确切地增加或减少并不重要,我想要的是现有投票应该覆盖和抱歉,我错误地将您的评论标记为我不知道标志的含义,我认为这是出于有用的目的,如何将其还原。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-06-19
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-12-10
相关资源
最近更新 更多