OrderBy not working in Laravel 5

I am using the following query. orderBy doesn't work in the following request. This request works on localhost, but it doesn't work on Online Server.

return DB::table('reports')
        ->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
        ->select('*')
        ->orderBy('report_id', 'desc')
        ->take(10)
        ->get();

      

+3


source to share


3 answers


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

If there is in both tables report_id

, it won't know which one to use and will probably throw an error if you're looking 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();

      

+2


source


Try it. use "reports.report_id";



return DB::table('reports')
        ->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
        ->select('*')
        ->orderBy('reports.report_id', 'desc')
        ->take(10)
        ->all();

      

+2


source


Try using reports.report_id like

return DB::table('reports')
        ->leftJoin('sources', 'reports.report_source_id', '=', 'sources.id')
        ->select('*')
        ->orderBy('reports.report_id', 'desc')
        ->take(10)
        ->get();

      

0


source







All Articles