orderBy is not working in Laravel 5

13,766

Solution 1

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();

Solution 2

Try this.. 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();
Share:
13,766
abu abu
Author by

abu abu

Updated on July 16, 2022

Comments

  • abu abu
    abu abu almost 2 years

    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();