如何在使用elequent模型laravel时获取数据DESC顺序并创建分页

时间:2017-09-19 19:48:39

标签: php mysql laravel pagination

我正在编写此代码以按顺序获取数据但不能正常工作

$newspaper_more_info = newspaper_jobad::with('sector', 'newspaper', 'province', 'test', 'catagory')->where('test_id', $job_test->id)->orderBy('id', 'DESC')->get();

这个代码用于创建分页并以desc顺序获取数据,但它会引发致命错误

$newspaper_more_info = newspaper_jobad::with('sector', 'newspaper', 'province', 'test', 'catagory')->where('test_id', $job_test->id)->orderBy('id', 'DESC')->paginate(15)->get();

1 个答案:

答案 0 :(得分:0)

在执行排序时,可能会混淆从哪个表中获取id。你能否按顺序

提及表名

喜欢$newspaper_more_info = newspaper_jobad::with('sector', 'newspaper', 'province', 'test', 'catagory')->where('test_id', $job_test->id)->orderBy('newspaper_jobad.id', 'DESC')->get()

因为它急于加载表格

如果laravel 5.4尝试

->orderByDesc('id')->get()