How to Create Custom Model Events in Laravel?
Hi,
In this short tutorial we will cover an laravel custom model events. i would like to show you how to create custom model events in laravel. i explained simply step by step laravel create custom model event. i would like to share with you laravel model event custom functions. Alright, let’s dive into the steps.
we can create custom model event in laravel 6, laravel 7, laravel 8, laravel 9, laravel 10 and laravel 11 version.
Sometime, we need to add specific task when your post or product will status become active, when it become active you need to send email or update timestamps etc. so here i will give you very simple example here, when product status will change active then we will update activated_at timestamp and send email using model custom event.
let's see example:
Create Product Model
app/Models/Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
use HasFactory;
protected $fillable = [
'name', 'price', 'status', 'activated_at'
];
protected $observables = ['activated'];
/**
* Write code on Method
*
* @return response()
*/
public function makeActive()
{
$this->update(['status' => 2]);
$this->fireModelEvent('activated', false);
}
}
Create Observer Class
Create observers class for Product. So, create bellow command:
php artisan make:observer ProductObserver --model=Product
app/Observers/ProductObserver.php
<?php
namespace App\Observers;
use App\Models\Product;
class ProductObserver
{
/**
* Handle the Product "created" event.
*
* @param \App\Models\Product $product
* @return void
*/
public function activated(Product $product)
{
$product->activated_at = now();
$product->save();
/*
You can perform what you want here:
\Log::info('Call Activated Event Function: '. $product->id);
\Mail::to($product->user->email)->view('some.email');
*/
}
}
Register Observers class on provider
app/Providers/EventServiceProvider.php
<?php
namespace App\Providers;
use Illuminate\Auth\Events\Registered;
use Illuminate\Auth\Listeners\SendEmailVerificationNotification;
use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use Illuminate\Support\Facades\Event;
use App\Observers\ProductObserver;
use App\Models\Product;
class EventServiceProvider extends ServiceProvider
{
/**
* The event listener mappings for the application.
*
* @var array
*/
protected $listen = [
Registered::class => [
SendEmailVerificationNotification::class,
],
];
/**
* Register any events for your application.
*
* @return void
*/
public function boot()
{
Product::observe(ProductObserver::class);
}
}
Create Controller Code
app/Http/Controllers/ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
class SignaturePadController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function index()
{
$product = Product::find(1);
$product->makeActive();
dd($product);
}
}
now you can try it and check it.
i hope it can help you...
Hardik Savani
I'm a full-stack developer, entrepreneur and owner of ItSolutionstuff.com. I live in India and I love to write tutorials and tips that can help to other artisan. I am a big fan of PHP, Laravel, Angular, Vue, Node, Javascript, JQuery, Codeigniter and Bootstrap from the early stage. I believe in Hardworking and Consistency.
We are Recommending you
- Laravel Replicate Model with Relationships Example
- Laravel 8 Model Observers Tutorial Example
- Laravel Eloquent withSum() and withCount() Example
- Laravel Eloquent firstOrNew Example
- Laravel 8 Fullcalendar with Create|Edit|Delete Event Example
- Laravel Model Events Tutorial
- Laravel Eloquent Delete Record By ID Example
- Laravel Model Caching - Performance Boost Tutorial
- Laravel Improve Speed Performance using Model Caching Tutorial
- How to disable model timestamps in Laravel?
- How to Get Table Name from Model in Laravel?