Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

orderBy is not working in Laravel 5

I am using the below query. orderBy is not working in below query. This query is working in localhost but it is not working in Online Server.

return DB::table('reports')
        ->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
        ->select('*')
        ->orderBy('report_id', 'desc')
        ->take(10)
        ->get();
like image 209
abu abu Avatar asked Sep 18 '25 00:09

abu abu


1 Answers

Try setting an alias for each table and then using the required alias on the orderBy

If there is a report_id in both tables it will not know which one to use and is probably throwing an error if you look for it.

return DB::table('reports as r')
    ->leftJoin('sources as s', 'r.report_source_id', '=', 's.id')
    ->select('*')
    ->orderBy('r.report_id', 'desc')
    ->take(10)
    ->get();
like image 151
RiggsFolly Avatar answered Sep 19 '25 15:09

RiggsFolly