Reputation: 157
I am new to laravel. I want to know is there any particular better option to use in the following case in laravel. For an example I have a table called product and another table called productlog. While a product is created / updated / deleted in those cases I have to insert a log in productlog table. I know, I can insert these data directly in the controller while these actions happening. Except this laravel has any better feature to use in this case?
Upvotes: 2
Views: 2641
Reputation: 157
I added this in my
app\Providers\AppServiceProvider.php
Product::created(function ($model) {
$producthistory = new Producthistory();
$producthistory >product_id = $model->id;
$producthistory >action = ' Product Created';
$producthistory >log = "Product created bla bla bla";
$producthistory >action_by = Auth::id();
$producthistory >save();
});
Upvotes: 0
Reputation: 2059
You want to perform some action while your Eloquent model is processing. Laravel’s Eloquent provide a convenient way to add your own action while the model is completing or has completed some action.
This is the list of all of the events, eloquent model fired that we can hook into:
For more details check the Laravel documentation here. Observers/ Model Events
Upvotes: 3