Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore.

15 St Margarets, NY 10033
(+381) 11 123 4567



“One of Many” Eloquent Relationship Added to Laravel

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

Continuing the example of a User model that has many logins, hre’s a code example of defining a one-of-many realationship using Eloquent:

class User extends Model
    public function latest_login()
        return $this->hasOne(Login::class)->ofMany();

And here’s the interface associated with ofMany:

interface SupportsPartialRelations
     * Indicate that the relation is a single result of a larger one-to-many relationship.
     * @param  string|array|null  $column
     * @param  string|Closure|null  $aggregate
     * @param  string|null  $relation
    public function ofMany($column = 'id', $aggregate="MAX", $relation = null);

     * Determine whether the relationship is a one-of-many relationship.
     * @return bool
    public function isOneOfMany();

The PR includes two helper methods if you are looking for more expressiveness in the relationship:


Documentation is forthcoming soon, so the best place to learn more is the description and discussion in Pull Request #37362. The description outlines drawbacks from using hasOne for this type of relationship, which should be helpful for your understanding of when to use this type of relationship.

The author of the PR also provided a GitHub repo with ofMany() examples: cbl/laravel-one-of-many. Specifically, check out the User model.

Credit: Source link

Previous Next
Test Caption
Test Description goes like this