HDTuto.com

Laravel Create Custom Model Event Example



Hi,In this tutorial, i will show you laravel model event custom functions. i would like to share with you create custom model events laravel. i explained simply about laravel custom model events. In this article, we will implement a how to create custom model events in laravel. follow bellow step for laravel create custom model event.

we can create custom model event in laravel 6, laravel 7 and laravel 8 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...




April 8, 2021 | Category : Other



Blog Search