Tricks for Laravel Eloquent Relationships

a strength of laravel is the elegant syntax. if you have a lot of models and relations in laravel (due to a lot of tables in the database), traversing can often end up in less elegant code. over the time, three small extensions have proven to be very useful for me, which i want to introduce briefly below. for this purpose, we only use some inheritance, magic 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

Thus, for example, this call is also possible in the example above:

b45e29b9125c2ab565b5526442437686

Check empty objects

If you want to output the name of the country for a personal address, you often see the following code:

b45e29b9125c2ab565b5526442437686

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

b45e29b9125c2ab565b5526442437686

This will also make the call 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

Then we create the file app\Helpers\ConvenienceCollection.php, which makes sure that the call of unknown functions for collections executes the function for all items of 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 call sorts only by location, although this was probably not desired:

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