laravel挂钩进入Eloquent前后保存为每个模型

时间:2013-02-15 19:48:07

标签: php laravel eloquent

我是Laravel和ORM的新手。在保存任何模型之前和之后,我怎么能挂钩进入Eloquent来解雇代码?我知道我可以针对特定型号进行以下操作,但我正在考虑如何为每个型号确定如何做到这一点。

class Page extends Eloquent {

   public function save()
   {
      // before save code 
      parent::save();
      // after save code
   }
}

3 个答案:

答案 0 :(得分:16)

甚至有更好的方法来实现这一目标!创建一个观察者,假设一个名为House的模型:

class HouseObserver {

    public function saving(House $house) {
        // Code before save
    }

    public function saved(House $house) {
        // Code after save
    }

}

现在通过在某处添加行HouseHouse::observe(new HouseObserver)模型注册观察者。该行可以添加到模型的引导方法中:

class House extends Eloquent {

    // Lots of model code

    public static function boot() {
        parent::boot();
        self::observe(new HouseObserver);
    }

}

可以找到更多信息here

答案 1 :(得分:10)

使用laravel模型自己的生命周期事件可以解决这个问题

/**
 * model life cycle event listeners
 */
public static function boot(){
    parent::boot();

    static::creating(function ($instance){
        //
    });

    static::created(function ($instance){
        //
    });
}

答案 2 :(得分:3)

您可以创建一个扩展eloquent的BaseModel类,然后让所有模型扩展BaseModel。这是一个例子:

abstract class Elegant extends Eloquent{

/* Save ****************************/
public function preNew() {}
public function postNew() {}
public function preSave() { return true; }
public function postSave() {}
public function save($validate=true, $preSave=null, $postSave=null)
{
    $newRecord = !$this->exists;
    if ($validate)
        if (!$this->valid()) return false;
    if($newRecord)
        $this->preNew();

    $before = is_null($preSave) ? $this->preSave() : $preSave($this);
      // check before & valid, then pass to parent
    $success = ($before) ? parent::save() : false;
    if ($success)
        is_null($postSave) ? $this->postSave() : $postSave($this);
    if($newRecord)
        $this->postNew();
    return $success;
}
public function onForceSave(){}
public function forceSave($validate=true, $rules=array(), $messages=array(), $onForceSave=null)
{
    if ($validate)
        $this->valid($rules, $messages);
     $before = is_null($onForceSave) ? $this->onForceSave() : $onForceSave($this);  // execute onForceSave
     return $before ? parent::save() : false; // save regardless of the result of validation
}

/** Soft Delete ****************************/
public function preSoftDelete() {  return true;  }
public function postSoftDelete()  { }
public function softDelete($val = true, $preSoftDelete=null, $postSoftDelete=null)
{
    if ($this->exists)
    {
        $before = is_null($preSoftDelete) ? $this->preSoftDelete() : $preSoftDelete($this);
        $success = null;
        if($before) {
            $this->set_attribute(static::$softDelete, $val);
            $success = $this->save(false);
        }
        else
            $success = false;
        if ($success)
        {
            is_null($postSoftDelete) ? $this->postSoftDelete() : $postSoftDelete($this);
         }
        return $success;
    }
}

/** Hard Delete ****************************/
public function preDelete()  { return true;}
public function postDelete(){}
public function delete( $preDelete=null, $postDelete=null)
{
    if ($this->exists)
    {
        $before = is_null($preDelete) ? $this->preDelete() : $preDelete($this);
        $success = ($before) ? parent::delete() : false;
        if ($success)
        {
            is_null($postDelete) ? $this->postDelete() : $postDelete($this);
         }
        return $success;
    }
}
}