Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to use **distinct on()** in laravel's query builder if using Postgre? [closed]

i want the syntax for distinct on() in laravel query builder.

like image 910
Bakhtawar GIll Avatar asked Sep 11 '25 20:09

Bakhtawar GIll


1 Answers

Eloquent does not have a distinctOn function.

You could use a raw query

$query->select(
    \DB::raw("DISTINCT ON (field1, field2) *")
);

or install a package that adds the distinctOn method, such as this one.

If you're using Laravel 9 or above - native distinct on support is available, as long as you're using the Postgres driver.

$query->distinct(['field1', 'field2']);
like image 90
piscator Avatar answered Sep 14 '25 10:09

piscator