最新和最后回复另外一种做法
public function index(Request $request)
{
$order = $request->order == 'recent' ? 'order': 'created_at';
$topics = Topic::with('user','category')->orderBy($order,'desc')->paginate(30);
return view('topics.index', compact('topics'));
}
这样就可以省去写 scope 了,不过只是提供一些思路啦,遇到其他的也可以这样做。
这个敲这个教程的第四遍吧,确实一遍比一遍好很多。大家加油。
推荐文章: