Skip to content

Instantly share code, notes, and snippets.

@GhazanfarMir
Created July 6, 2017 14:47
Show Gist options
  • Save GhazanfarMir/2a4a2cbaa45c5192cf37847e206cf208 to your computer and use it in GitHub Desktop.
Save GhazanfarMir/2a4a2cbaa45c5192cf37847e206cf208 to your computer and use it in GitHub Desktop.
Laravel: How to create custom model events
<?php
// App\Address Model
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Observers\AddressObserver; // included our Model Observer
/**
* Class Address
* @package app
*/
class Address extends Model
{
/**
* Array of our custom model events declared under model property $observables
* @var array
*/
protected $observables = [
'published',
'unpublished',
];
/**
* Publish address & fire custom model event
*
*/
public function publish()
{
// logic to update Address status goes here
// fire custom event on the model
$this->fireModelEvent('published', false);
}
/**
* Unpublish address & fire custom model event
*
*/
public function unPublish()
{
// logic to update Address status goes here
// fire custom event on the model
$this->fireModelEvent('unpublished', false);
}
}
?>
<?php
// App\Observers
namespace App\Observers;
use App\Address; // model included to have access to data
use Illuminate\Support\Facades\Log; // Log included to log info
class AddressObserver
{
/**
* @param Address $address
*/
public function published(Address $address)
{
// I am testing here by logging info into logs, you can do anything here,
// like send emails, insert into database, or notify via sms,
// all logic goes here
Log::info('Address is published ('.$address->name.')');
}
/**
* @param Address $address
*/
public function unpublished(Address $address)
{
// logic goes here
}
}
<?php
// App\Providers
namespace App\Providers;
use App\Address; // model
use App\Observers\AddressObserver; // model observer
use Illuminate\Support\ServiceProvider;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
// https://laravel.com/docs/5.4/eloquent#observers
// observer to group listeners into a single class,
Address::observe(AddressObserver::class);
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
@mknparreira
Copy link

It is not necessary to define use App\Observers\AddressObserver; // included our Model Observer on line 8 at the file Address.php

@andreshg112
Copy link

I'm doing the same as you but I get Method Illuminate\Database\Query\Builder::fireModelEvent does not exist.

@LeonAlvarez
Copy link

I'm doing the same as you but I get Method Illuminate\Database\Query\Builder::fireModelEvent does not exist.

Events are not in query builder, but the Eloquent Model thats why.

@jpkleemans
Copy link

jpkleemans commented Nov 24, 2020

You could also use attribute events to automatically fire events on change of the status column:

class Address extends Model
{
    protected $dispatchesEvents = [
        'status:published' => AddressPublished::class,
        'status:unpublished' => AddressUnpublished::class,
    ];
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment