i want the syntax for distinct on() in laravel query builder.
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']);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With