$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'));
}
关于 LearnKu
建议看一下这章下面的其他讨论,一年前和两年前已经有人讨论过和你相同的问题了
分享:scopeWithOrder ($query, $order), $query 是怎么来的?
问答: scopeWithOrder ($query, $order),$query 是什么东西来的??