datajoe/postgresql-distinct-on
Adds PostgreSQL DISTINCT ON support to Laravel's query builder and Eloquent, so you can fetch the first row per group with a clean, chainable API. Ideal for latest-per-user or per-category queries without raw SQL or subqueries.
current_* table maintained by triggers/jobs. It reduces the pressure to add materialized "latest" tables early, which are expensive to reverse.Consider it when:
selectRaw('DISTINCT ON (...)') — especially because Laravel wraps queries for count() in pagination, which breaks naive raw-prefix hacks.whereIn plus per-group lookups, or a nightly rebuild of a "latest" table.Look elsewhere or do not adopt when:
ROW_NUMBER() OVER (PARTITION BY ...) instead: portable, supported by Postgres, and self-owned.Executives: This is a small, targeted capability that lets us build "latest per customer/account/device" views in the database instead of in application code. It cuts database round-trips and page latency for a common class of screen — dashboards, admin lists, activity feeds — and costs essentially nothing to adopt: no vendor contract, no infrastructure change, no schema migration. The trade-off is a low-maintenance open-source dependency. We mitigate by pinning the version, covering it with our own tests, and keeping the option to vendor the roughly 50 lines ourselves. Recommendation: adopt as a scoped utility, not a platform-level dependency.
Engineering: It's a query-builder and grammar extension adding distinctOn() for Postgres, keeping queries composable with orderBy, pagination, and eager loading instead of relying on raw selectRaw prefixes that break when Laravel wraps the query for count(). Constraints are clear: Postgres-only, declared support stops at Laravel 8, and there is no maintainer signal (0 stars, 0 dependents, Bitbucket-hosted). Plan: validate against our Laravel and Postgres versions, wrap it behind our own macro or repository method so call sites don't depend on the package API, add regression tests for pagination and count(), and keep a fork path open. If version support doesn't line up, the fallback is a window-function implementation of the same query — portable, self-owned, and functionally equivalent for our use cases.
How can I help you explore Laravel packages today?