Skip to content

Commit

Permalink
完成了用户点赞评论的功能
Browse files Browse the repository at this point in the history
  • Loading branch information
Administrator committed Mar 7, 2018
1 parent 7f91869 commit 4285a03
Show file tree
Hide file tree
Showing 14 changed files with 457 additions and 240 deletions.
39 changes: 39 additions & 0 deletions app/Http/Controllers/CommentController.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@
namespace App\Http\Controllers;

use App\Model\Comment;
use App\Model\Zan;
use Request;
use Validator;
use Auth;

class CommentController extends Controller
{
Expand Down Expand Up @@ -40,4 +42,41 @@ public function doComment(){
];
}
}
//点赞评论
public function zan($comment_id){
$zan = new Zan();
$zan->user_id = Auth::id();
$zan->comment_id = $comment_id;
$status = $zan->save();
if ($status){
return [
'error'=>'1',
'msg'=>''
];
}else{
return [
'error'=>'0',
'msg'=>'点赞失败,请稍后重试'
];
}

}
//取消赞评论
public function unzan($comment_id){

$status = Zan::where('user_id',Auth::id())->where('comment_id',$comment_id)->delete();

if ($status){
return [
'error'=>'1',
'msg'=>''
];
}else{
return [
'error'=>'0',
'msg'=>'取消赞失败,请稍后重试'
];
}

}
}
10 changes: 1 addition & 9 deletions app/Http/Controllers/CommonController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,5 @@ public function file_up()
}
return json_encode($result); //返回必须为json格式
}
//帖子浏览次数+1
public function set_hits()
{
$id = request('id');
$post = Post::findOrFail($id);
$renqi = $post->renqi;
$post->renqi = $renqi + 1;
$post->save();
}

}
10 changes: 9 additions & 1 deletion app/Http/Controllers/PostController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Http\Controllers;

use App\Model\Zan;
use Validator;
use App\Model\Category;
use App\Model\Post;
Expand All @@ -19,6 +20,11 @@ public function add(){
//帖子详情
public function show($id)
{
$post = Post::findOrFail($id);
$renqi = $post->renqi;
$post->renqi = $renqi + 1;
$post->save();

$post = Post::where('id',$id)->first();
return view('home.post.detail',compact('post'));
}
Expand All @@ -28,7 +34,7 @@ public function store()
{
$validator = Validator::make(request()->all(),[
'category' => 'required',
'title' => 'required|min:5|max:20',
'title' => 'required|min:5|max:50',
'my-editormd-html-code' => 'required',
'reward' => 'required|integer',
]);
Expand All @@ -49,4 +55,6 @@ public function store()
}
}



}
14 changes: 14 additions & 0 deletions app/Model/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ public function post(){
return $this->belongsTo('App\Model\Post','post_id','id');
}

/**
* 判断用户是否赞了这条评论
*/
public function zan($user_id){
return $this->hasOne(\App\Model\Zan::class)->where('user_id',$user_id);
}

/**
* 评论所有的赞
*/

public function zans(){
return $this->hasMany('App\Model\Zan','comment_id','id');
}


}
2 changes: 2 additions & 0 deletions app/Model/Post.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,6 @@ public function comments()
return $this->hasMany('App\Model\Comment')->orderBy('created_at','desc');
}



}
12 changes: 12 additions & 0 deletions app/Model/Zan.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace App\Model;

use Illuminate\Database\Eloquent\Model;

class Zan extends Model
{
protected $fillable = [
'user_id','comment_id'
];
}
Loading

0 comments on commit 4285a03

Please sign in to comment.