$topics = $topic->withOrder($request->order)为什么只传了一个参数?
public function scopeWithOrder($query, $order)是两个参数,为什么调用WithOrder时,只传入一个参数$request->order?
public function scopeWithOrder($query, $order)
{
// 不同的排序,使用不同的数据读取逻辑
switch ($order) {
case 'recent':
$query->recent();
break;
default:
$query->recentReplied();
break;
}
}
class TopicsController extends Controller
{
. . .
public function index(Request $request, Topic $topic)
{
$topics = $topic->withOrder($request->order)
->with('user', 'category') // 预加载防止 N+1 问题
->paginate(20);
return view('topics.index', compact('topics'));
}
推荐文章: