模型里面
<?php
namespace AppModels;
use IlluminateDatabaseEloquentModel;
class Post extends Model
{
//
protected $table = "posts";
public $primaryKey = 'id';
public function user()
{
return $this->belongsTo("AppModelsUser","user_id",'id');
}
public function comments()
{
return $this->hasMany('AppModelsComment','post_id','id')->orderBy("created_at",'desc');
}
}
控制器查询列表
$posts = Post::withCount("comments")->orderBy("created_at",'desc')->paginate(6);
视图里面使用
{{ $post->comments_count }}