laravel博客评论功能,数据表关联
2019-11-23 admin laravel 1470
博客采用了laravel6.*框架,加了简易的评论功能,对于个人博客来说已足够了。评论数据表迁移文件设计如下:
use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateComments extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('comments', function (Blueprint $table) { $table->increments('id'); $table->string('content'); $table->integer('blog_id'); //这条评论是针对哪一篇博客的? $table->integer('user_id'); //这条评论是哪一位用户发送的? $table->integer('blog_title'); //博客标题 $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('comments'); } }
模型关联,博客文件模型BlogModel.php
// 评论绑定1:n关系 public function commentes() { return $this->hasMany(Comment::class,'blog_id'); // 1 hasMany n //这里'blog_id'是关键,因为:Eloquent会基于模型决定外键名称,会自动假设 commentes 模型有一个 blog_model_id 字段,而我们的是blog__id,要覆盖这个决定,需要传递这个参数 }
评论模型Comment.php
namespace App\Models; use App\User; use App\Models\BlogModel; use Illuminate\Database\Eloquent\Model; class Comment extends Model { protected $fillable = [ 'content', 'user_id', 'blog_id', 'blog_title' ]; public function BlogModel() { return $this->belongsTo(BlogModel::class); // n belongsTo 1评论 } // 根据 user_id 获取用户名 public function userName() { return User::find($this->user_id)->name; //这里通过当前对象的 user_id 获取 user对象, 然后指向->name属性 } //根据user_id获取头像 public function userAvatar() { $avatar = User::find($this->user_id)->avatar; //这里通过当前对象的 user_id 获取 user对象, 然后指向->avatar属性 if ($avatar == NULL){ $avatar = '/uploads/avatar/default.jpg'; } return $avatar; } }
博客控制器:
namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\BlogModel; use App\Models\Comment; use Illuminate\Support\Facades\Auth; class ArticleController extends Controller { //首页 public function index() { if (Auth::check()) { $data = BlogModel::orderBy('created_at','desc')->simplePaginate(7);//以创建时间为倒序排列,取6条,分页 $suiji = BlogModel::inRandomOrder()->take(5)->get(['id', 'title','views'])->toArray();//随机取5篇文章 }else{ $data = BlogModel::where('sfkf','是')->orderBy('created_at','desc')->simplePaginate(7);//以创建时间为倒序排列,取6条,分页 $suiji = BlogModel::where('sfkf','是')->inRandomOrder()->take(5)->get(['id', 'title','views'])->toArray();//随机取5篇文章 } $tags = BlogModel::pluck('tag')->toArray();//获取所有分类标签,集合要转数组 $num = count($tags);//文章数量 $comments = Comment::orderBy('created_at','desc')->paginate(7);//评论 $plsl = count($comments);//评论数量 //$qctag = array_unique($tags);//去除重复的tag数组 $tagsz = array_count_values($tags);//计算每个tag的数量 return view('home.article.index', compact('data','num','tagsz','suiji','comments','plsl')); } //显示文章 public function show($id) { if (Auth::check()) { $data = BlogModel::find($id); $prev = BlogModel::select('id', 'title', 'tag')->orderBy('created_at', 'desc') ->where('id', '<', $id)->limit(1)->first();// 获取上一篇 $next = BlogModel::select('id', 'title', 'tag')->orderBy('created_at', 'asc') ->where('id', '>', $id)->limit(1)->first();// 获取下一篇 }else{ $data = BlogModel::where([['sfkf','是'],['id',$id]])->first(); $prev = BlogModel::select('id', 'title', 'tag')->orderBy('created_at', 'desc') ->where([['id', '<', $id],['sfkf','=','是']])->limit(1)->first();// 获取上一篇 $next = BlogModel::select('id', 'title', 'tag')->orderBy('created_at', 'asc') ->where([['id', '>', $id],['sfkf','=','是']])->limit(1)->first();// 获取下一篇 } if($data == null) {echo '文章不存在或无权访问!';exit;} //dd($data); $data['views'] = ($data['views']) + 1;//浏览次数+1 $data->timestamps = false;//不更新updated_at $data->save(); $tags = BlogModel::pluck('tag')->toArray();//获取所有分类标签,集合要转数组 $num = count($tags);//文章数量 $zxpl = Comment::orderBy('created_at','desc')->paginate(6);//最新评论 $plsl = count($zxpl);//评论数量 $tagsz = array_count_values($tags);//计算每个tag的数量 $comments = $data->commentes;//查询评论 //dd($comments); return view('home.article.list', compact('data','num','tagsz','comments','prev','next','plsl','zxpl')); } //文章分类 public function tag($tag) { if (Auth::check()) { $data = BlogModel::where('tag',$tag)->orderBy('created_at','desc')->simplePaginate(7); $suiji = BlogModel::inRandomOrder()->take(5)->get(['id', 'title','views'])->toArray();//随机取5篇文章 }else{ $data = BlogModel::where([['tag',$tag],['sfkf','是']])->orderBy('created_at','desc')->simplePaginate(7); $suiji = BlogModel::where('sfkf','是')->inRandomOrder()->take(5)->get(['id', 'title','views'])->toArray();//随机取5篇文章 } $tags = BlogModel::pluck('tag')->toArray();//获取所有分类标签,集合要转数组 $num = count($tags);//文章数量 $comments = Comment::orderBy('created_at','desc')->paginate(6);//评论 $plsl = count($comments);//评论数量 $tagsz = array_count_values($tags);//计算每个tag的数量 return view('home.article.index', compact('data','num','tagsz','suiji','comments','plsl')); } //搜索文章 public function search(Request $request) { $wd = $request->input('wd'); if (Auth::check()) { $data = BlogModel::where('title','like' ,"%$wd%")->orderBy('created_at', 'desc')->simplePaginate(7); $suiji = BlogModel::inRandomOrder()->take(5)->get(['id', 'title','views'])->toArray();//随机取5篇文章 }else{ $data = BlogModel::where([['title','like' ,"%$wd%"],['sfkf','是']])->orderBy('created_at', 'desc')->simplePaginate(7); $suiji = BlogModel::where('sfkf','是')->inRandomOrder()->take(5)->get(['id', 'title','views'])->toArray();//随机取5篇文章 } $tags = BlogModel::pluck('tag')->toArray();//获取所有分类标签,集合要转数组 $num = count($tags);//文章数量 $comments = Comment::orderBy('created_at','desc')->paginate(6);//评论 $plsl = count($comments);//评论数量 $tagsz = array_count_values($tags);//计算每个tag的数量 return view('home.article.index', compact('data','num','tagsz','suiji','comments','plsl')); } }
路由设计:
Route::resource('/blog', 'ArticleController');//博客 Route::get('/tag/{tag}', 'ArticleController@tag')->name('tag');//博客tag页 Route::get('/bksearch', 'ArticleController@search')->name('bk.search'); //搜索文章 Route::resource('comment', 'CommentController', ['only' => 'store']);//评论
前台文件:
今天准备将博客文章和评论表合并到一起,因此先将原代码备份到此。