Tricks for Laravel Eloquent Relationships

One of Laravel 's strengths is its elegant syntax. If you have a lot of models and relationships in Laravel (due to a lot of tables in the database), traversing can often end in less elegant code. Over time, three small extensions have proven successful for me, which I would like to briefly introduce below. To do this we just use some inheritance, magical methods and custom collections.


First we create a new model under app\ConvenienceModel.php :

b45e29b9125c2ab565b5526442437686

Then we let all models inherit from our new ConvenienceModel:

b45e29b9125c2ab565b5526442437686

Mixing your own functions with relationships

If you want to get connected models, you use the standard syntax to call them:

b45e29b9125c2ab565b5526442437686

If you now add your own business logic, you like to use the (lower) camel case notation:

b45e29b9125c2ab565b5526442437686

We want to unify the syntax of both variants by adding the magic method __call to the ConvenienceModel :

b45e29b9125c2ab565b5526442437686

This call is also possible in the example above:

b45e29b9125c2ab565b5526442437686

Check empty objects

If you want to output the country name for a person's address, you often see the following code:

b45e29b9125c2ab565b5526442437686

To prevent this, we include the stringhelper library and return a special object of the empty class `empty` if a model is not found.:

b45e29b9125c2ab565b5526442437686

This means that the call is successful (in this example, if something is found, the name of the country is output, in all other cases an empty string):

b45e29b9125c2ab565b5526442437686

If we also want to cover the case that the user with the ID 42 possibly does not even exist, we build a small helper function:

b45e29b9125c2ab565b5526442437686

Thus, the following call returns an empty string even if the user does not exist at all:

b45e29b9125c2ab565b5526442437686

Conveniently traverse n: m relations

Finally we want to simplify the following code:

b45e29b9125c2ab565b5526442437686

For this we use the possibility of custom collections . We are first expanding the convenience model:

b45e29b9125c2ab565b5526442437686

Next, we create the file app\Helpers\ConvenienceCollection.php , which ensures that when unknown functions are called in Collections, the function is executed for all items in the collection.:

b45e29b9125c2ab565b5526442437686

So we can use the following code to output all names of all countries of all addresses of all parents of the person:

b45e29b9125c2ab565b5526442437686

But also calls like the following are possible:

b45e29b9125c2ab565b5526442437686

Sort by multiple columns

The following command sorts only by location , although that was probably not the intended outcome.:

b45e29b9125c2ab565b5526442437686

To enable sorting by multiple columns, we are expanding our ConvenienceCollection:

b45e29b9125c2ab565b5526442437686

This allows us to sort the collection by several columns with:

b45e29b9125c2ab565b5526442437686

Standard sorting

We are expanding the ConvenienceCollection with a further help function:

b45e29b9125c2ab565b5526442437686

Now you can also give each model an individual standard sorting function:

b45e29b9125c2ab565b5526442437686

This allows us to sort a collection with:

b45e29b9125c2ab565b5526442437686

Object designations

Each model implements the getLabel method (getName varies depending on the model):

b45e29b9125c2ab565b5526442437686

This allows you to output the name of an object quickly and easily, and then you can implement the helper method sortByLabel in the ConvenienceCollection:

b45e29b9125c2ab565b5526442437686

Empty results

If an intermediate step is empty, the Laravel functions first() and last() normally return zero, so we introduce getFirst() and getLast() so that the following call always succeeds:

b45e29b9125c2ab565b5526442437686

Herewith we deliver back an __empty_helper that __x recognizes:

b45e29b9125c2ab565b5526442437686

Back