ItSolutionStuff.com

How to Create Custom Model Events in Laravel?

By Hardik Savani β€’ April 16, 2024
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...

Tags: Laravel
Hardik Savani

Hardik Savani

I'm a full-stack developer, entrepreneur, and founder of ItSolutionStuff.com. Passionate about PHP, Laravel, JavaScript, and helping developers grow.

πŸ“Ί Subscribe on YouTube

We Are Recommending You

β˜…

Laravel Replicate Model with Relationships Example

Read Now β†’
β˜…

Laravel 8 Model Observers Tutorial Example

Read Now β†’
β˜…

Laravel Eloquent withSum() and withCount() Example

Read Now β†’
β˜…

Laravel Eloquent firstOrNew Example

Read Now β†’
β˜…

Laravel 8 Fullcalendar with Create|Edit|Delete Event Example

Read Now β†’
β˜…

Laravel Model Events Tutorial

Read Now β†’
β˜…

Laravel Eloquent Delete Record By ID Example

Read Now β†’
β˜…

Laravel Model Caching - Performance Boost Tutorial

Read Now β†’
β˜…

Laravel Improve Speed Performance using Model Caching Tutorial

Read Now β†’
β˜…

How to disable model timestamps in Laravel?

Read Now β†’
β˜…

How to Get Table Name from Model in Laravel?

Read Now β†’