【发布时间】: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
votesleft joinusersonusers.id=votes.user_idleft joinpostsonposts.id=votes.post_idwhere (votes.hide= 0 andvotes@.@.@.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