Created
July 6, 2017 14:47
-
-
Save GhazanfarMir/2a4a2cbaa45c5192cf37847e206cf208 to your computer and use it in GitHub Desktop.
Laravel: How to create custom model events
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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); | |
} | |
} | |
?> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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() | |
{ | |
// | |
} | |
} |
I'm doing the same as you but I get Method Illuminate\Database\Query\Builder::fireModelEvent does not exist.
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.
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
It is not necessary to define use App\Observers\AddressObserver; // included our Model Observer on line 8 at the file Address.php