Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent model overriding static boot method

Tags:

php

laravel

I want to override model events and found this example code but am not sure I understand it completely.

SOURCE:

http://driesvints.com/blog/using-laravel-4-model-events/

There is a static method with another static method in it...How does that work? Or is it setting a static property in the boot method somehow?

<?php

class Menu extends Eloquent {
    protected $fillable = array('name', 'time_active_start', 'time_active_end', 'active');

    public $timestamps = false;

    public static $rules = array(
        'name' => 'required',
        'time_active_start' => 'required',
        'time_active_end' => 'required'
    );

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

       static::saving(function($post)
       {

       });       
   }    

}
like image 840
Chris Muench Avatar asked Dec 29 '25 08:12

Chris Muench


1 Answers

static::saving() just calls the static method saving on itself (and parent classes if not existent in current class). So it is essentially doing the same as:

Menu::saving(function($post){

});

So it is registering a callback for the saving event within the boot function.

Laravel documentation on model events

like image 115
lukasgeiter Avatar answered Dec 31 '25 22:12

lukasgeiter