By extending Eloquent we can inherits all of the existing functionality to our model. By Extending newly created Base Model we can inherits custom methods in all models. By placing global used common methods at one place we can satisfy DRY principle.
To extend Eloquent, all you have to do is create a new class which extends Eloquent.I am going to name my model as BaseModel, so lets create BaseModel.php in your app/models directory.
In the below BaseModel, we are firing events while creating and updating records, and we are inserting and updating “created_by” and “updated_by” columns receptively.
How to use
For Example if you have a model called post.php
Laravel will not boot the model until it is needed because laravel uses lazy auto-loading technique. That’s it.
I hope you like this Post, Please feel free to comment below, your suggestion and problems if you face - we are here to solve your problems.
I am Arjun from Hyderabad (India). I have been working as a software engineer from the last 7+ years, and it is my passion to learn new things and implement them as a practice. Aside from work, I like gardening and spending time with pets.