Use laravel eloquent traits to avoid code duplication

Muhammed ElFeqy
3 min readOct 20, 2021
Use laravel eloquent traits to avoid code duplications

Consider that you have a particular event that you need to fire in several places, such as deleting the model’s related image after the deletion of the model itself.

you may use the same code for posts , products , categories , ..etc.

it would be a headache to take care of this each time you need to delete a model.

so, how we can avoid this?

Use Eloquent events

laravel eloquent ships with several events, it allows you to do a particular task at the exact time you need.

in our example (delete the model’s related images when the model delete).

you can read more about Eloquent events In the laravel official Docs

to reach that, you can use the boot static method in your model :

use model events to achieve a specific goal on a specific event

now, you don’t have to do anything while you’re deleting a post (Model) . laravel will take care of that by firing an event to delete the images each time you try to delete a post.

Ok!, you’re doing great so far, you are just made your code much cleaner by delegating the image deletion logic into the model itself.

but we still have that code duplication problem right?. every time you want to add the same event (deleting the model’s image), you have to copy the same code in that particular model.

and as you already know, this is bad in software development, you’ll have to edit each model once you have an improvement to this piece of code.

so you should make your code DRY (do not repeat yourself) as much as possible.

here, the Bootable Traits are having our backs.

Bootable Traits

you can create a trait that has the same logic and use it wherever you need it.

Just create a static method with a name boot{TraitName} (replace {TraitName} with your actual trait name). it will be executed as a boot() method on your Eloquent model.

here’s an example:

Bootable traits can help you to avoid code duplicates

As you can see, we encapsulate the logic of deleting the image in a single bootable trait, and now it’s a reusable component. just use the same trait DeleteModelImages with any model you need.

now you can delete any post without worrying about deleting its images, you can leave that for laravel to handle.

in the end, I hope this article was helpful for you, and that you’ve learned something today, if you have any comments just please let me know in the comments.

You can also follow me on Twitter for more larevel tips.

-Muhammed Elfeqy

--

--

Muhammed ElFeqy

Hello devs👋, I’m a developer sharing my thoughts (📝) during my journey as a developer. so, let’s </code>.