Extend Eloquent Model with created_by, updated_by ?

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.

0 0 vote
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments
3 years ago
marlon manuel
marlon manuel
2 years ago

Hola que tal he tratado de usar es implementación en laravel 5.7
con el metodo Model::updateorCreate(
pero no funciona alguna recomendacion
ni siquiera entra el metodo :creating() o updating();

DMCA.com Protection Status
Would love your thoughts, please comment.x