php - Laravel 4 - Trouble overriding model's save method -


i'm trying override post class's save() method can validate of fields saved record:

// user.php <?php  class post extends eloquent {     public function save()     {         // code before save         parent::save();          //code after save     } } 

when try , run method in unit testing following error:

..{"error":{"type":"errorexception","message":"declaration of post::save() should compatible of illuminate\\database\\eloquent\\model::save()","file":"\/var\/www\/laravel\/app\/models\/post.php","line":4}} 

create model.php class extend in self-validating models

app/models/model.php

class model extends eloquent {      /**      * error message bag      *       * @var illuminate\support\messagebag      */     protected $errors;      /**      * validation rules      *       * @var array      */     protected static $rules = array();      /**      * validator instance      *       * @var illuminate\validation\validators      */     protected $validator;      public function __construct(array $attributes = array(), validator $validator = null)     {         parent::__construct($attributes);          $this->validator = $validator ?: \app::make('validator');     }      /**      * listen save event      */     protected static function boot()     {         parent::boot();          static::saving(function($model)         {             return $model->validate();         });     }      /**      * validates current attributes against rules      */     public function validate()     {         $v = $this->validator->make($this->attributes, static::$rules);          if ($v->passes())         {             return true;         }          $this->seterrors($v->messages());          return false;     }      /**      * set error message bag      *       * @var illuminate\support\messagebag      */     protected function seterrors($errors)     {         $this->errors = $errors;     }      /**      * retrieve error message bag      */     public function geterrors()     {         return $this->errors;     }      /**      * inverse of wassaved      */     public function haserrors()     {         return ! empty($this->errors);     }  } 

then, adjust post model.
also, need define validation rules model.

app/models/post.php

class post extends model {     // validation rules     protected static $rules = [         'name' => 'required'     ]; } 

controller method
model class, post model automaticaly validated on every call save() method

public function store() {     $post = new post(input::all());      if ($post->save())     {         return redirect::route('posts.index');     }      return redirect::back()->withinput()->witherrors($post->geterrors()); } 

this answer based on jeffrey way's laravel model validation package laravel 4.
credits man!


Comments

Popular posts from this blog

Issues changing the value of an element in an array in matlab -

php - MySQLi binding parameters in a prepared statement doesn't work unless inserted after "WHERE" -

vb.net - Alternative to the T-SQL AS keyword -