Stripping empty attributes from Laravel model before saving

I’m trying to strip all empty fields from being submitted into my database from the form (using Mongo – Moloquent extends Eloquent).

I’ve got a base model:

class Base extends Moloquent {
  public static function boot(){
    parent::boot();
    static::saving( function($model){
      $arr = $model->toArray();
      $removed = array_diff($arr, array_filter($arr));
      foreach($removed as $k => $v) $model->__unset($k);
      return true;
    });
  }
}

And then extend it:

class MyModel extends Base{
  public static function boot(){
    parent::boot()
  }
}

But it has no effect on the child class (MyModel); I think I’m just missing something obvious that my [current] tunnel vision won’t let me see.


Source: laravel

Leave a Reply