我试图在
的基础上对一个简单的查询进行排序created_at
但没有任何作用 我的查询
$newspaper_more_info = newspaper_jobad::with('sector', 'newspaper', 'province', 'test', 'catagory')->where('test_id', $job_test->id)->paginate(15)
我试过
$newspaper_more_info = newspaper_jobad::with('sector', 'newspaper', 'province', 'test', 'catagory')->where('test_id', $job_test->id)->paginate(15)->sortBy('created_at');
$newspaper_more_info = newspaper_jobad::with('sector', 'newspaper', 'province', 'test', 'catagory')->where('test_id', $job_test->id)->orderBy('created_at', 'desc')->paginate(15)
但不适合。 我也有一个问题是我正在使用急切加载或延迟加载我不知道我刚刚实现了这个但不知道确切的术语
答案 0 :(得分:1)
您可以在$newspaper_more_info = newspaper_jobad::with([
'sector' => function($query) {
$query->orderBy('created_at'); // This will sort the 'sector' relationship by the created_at column.
}, 'newspaper', 'province', 'test', 'catagory'
])->where('test_id', $job_test->id)->orderBy('created_at', 'desc')->paginate(15)
调用中将闭包作为值传递,以便为急切加载的SQL添加约束。 You can read about it here under the title "Constraining Eager Loads"
Time On Task