0
我正在構建一個論壇。每當用戶留下回復時,我都希望將發佈的主題置於頂端。對於沒有回覆的主題,我想通過created_at
列來訂購它們。在論壇索引頁面上將最新的評論主題置於頂端
你是怎麼做的?
論壇控制器
public function index()
{
$categories = Category::all();
$topics = Topic::with(['comments' => function ($query) {
$query->orderBy('comments.created_at', 'desc');
}])->paginate(20);
}
這是我演講的題目表
Schema::create('topics', function (Blueprint $table) {
$table->increments('id');
$table->integer('category_id')->unsigned();
$table->integer('user_id')->unsigned();
$table->string('title');
$table->text('body');
$table->timestamps();
});
這裏是我的評語表
$table->increments('id');
$table->text('reply');
$table->integer('user_id')->unsigned();
$table->integer('topic_id')->unsigned();
$table->foreign('topic_id')->refrenced('id')->on('topics')->onDelete('cascade');
$table->timestamps();
評論模型
class Comment extends Model
{
protected $fillable = [
'reply',
'user_id',
'topic_id'
];
public function topic()
{
return $this->belongsTo('App\Topic');
}
public function user()
{
return $this->belongsTo('App\User');
}
}
主題模型
class topic extends Model
{
protected $fillable = [
'title',
'body',
'category_id'
];
public function category()
{
return $this->belongsTo('App\category');
}
public function user()
{
return $this->belongsTo('App\User');
}
public function comments()
{
return $this->hasMany('App\Comment');
}
}
仍在試圖弄清楚這一點。任何幫助將非常感謝!
感謝您reply.I曾試過和L離開了我的第二個帖子的回覆,看看如果主題是第一位的索引頁上,但沒有工作。 – shigg