Support the ongoing development of Laravel.io →
Eloquent

Hi every body. I have a directory under my app folder named observers and I listen for various events such as created , updated , ... and I handle them ! my observer events bootstrap is in my model boot function ! for example I have User Model under models folder and I have UserObserver under observers folder! now I need to add my specific event to eloquent observer ! consider I want fire event when one column such as "enable" of my user table has changed. I know I should extend eloquent Model and add a function like userchangestate() coz I already have looked up Eloquent Model it has a function for every event ('creating' , 'created' , 'saving' , 'saved' , ....) but still I'm not sure how handle it ! thanks !

Last updated 3 years ago.
0

thanks to http://stackoverflow.com/users/1010632/david-barker now I got the answer : this is my observer :

class NewsObserver {

protected $events;

public function __construct(Dispatcher $dispatcher)
{
    $this->events = $dispatcher;
}

public function saving($model)
{
    if ($model->isDirty(['publish']))
    {
        $this->changeState($model->publish);
    }
}	

public function changeState($value)
{
    echo 'event fired and I got it';
}

}

and this is my model

use Illuminate\Events\Dispatcher; class News extends Magniloquent {

protected $fillable = [];
public $table = 'news';


public static function boot()
{
    parent::boot();

    News::observe(new NewsObserver(new Dispatcher));
}	

}

Last updated 3 years ago.
0

Sign in to participate in this thread!

PHPverse

Your banner here too?

Moderators

We'd like to thank these amazing companies for supporting us

Your logo here?

Laravel.io

The Laravel portal for problem solving, knowledge sharing and community building.

© 2025 Laravel.io - All rights reserved.