Ad

How To Make A Distinct In Only One Table Of A Join Using Laravel Query Builder?

- 1 answer

I have this query... and I would like to make a distinct only in (tbl_holdings)

This is my query

$holdings = DB::table('tbl_perimetros')
                ->distinct()
                ->join('tbl_holdings', 'tbl_holdings.id', '=', 'tbl_perimetros.holdings_id')
                ->get();
Ad

Answer

Use groupBy instead of distinct:

$holdings = DB::table('tbl_perimetros')
                ->groupBy('tbl_perimetros.holdings_id') 
                ->get();

Remove ONLY_FULL_GROUP_BY:

Add modes to the config/database.php

'mysql' => [
    ...
    'modes' => [
        'STRICT_ALL_TABLES',
        'ERROR_FOR_DIVISION_BY_ZERO',
        'NO_ZERO_DATE',
        'NO_ZERO_IN_DATE',
        'NO_AUTO_CREATE_USER',
    ],
],
Ad
source: stackoverflow.com
Ad