A new “one of many”  Eloquent  relationship is coming to Laravel 8.42 thanks a PR contribution by  Lennart Carstens-Behrens  along with collaboration from Taylor Otwell (and others) in  framework pull request #37362 :

The one-of-many relationship creates a one-to-one association from a one-to-many relationship. For example, “latest login,” “first login,” and product pricing (i.e., get the latest published price for a product).

The implementation works by filtering the intersection of the relation joined on itself. A query example from the pull request helps illustrate how it works under the hood:

FROM `logins`
    SELECT MAX(id) AS id
    FROM logins
    GROUP BY logins.user_id
) AS latest_login 
ON latest_login.id = logins.id

#news #laravel

"One of Many" Eloquent Relationship Added to Laravel
1.45 GEEK