Laravel Query Builder Join clause is ignoring orderBy()

  laravel, mysql, query-builder

I’m using Laravel query builder and I would like to ask how specify which row to use for the left join if many rows are returned?

I have code like that:

$builder->leftJoin('table2', function (JoinClause $join) {
    $join->on('table2.id', '=', "table1.id")
         ->on('table2.region', '=', "table1.region")
         ->orderBy('table2.updatedAt', 'DESC'); // This is being ignored.
});

The part $join->on()->on() returns multiple rows so I need to specify which one to use for the left join. I though I can order it by table2.updatedAt and then it will use the first row for the left join. But it is completly ignoring the orderBy. So I dont know how to get the one specific row.

Source: Laravel

Leave a Reply