Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 4 - Trouble overriding model's save method

I'm trying to override my Post class's save() method so that I can validate some of the fields that will be saved to the record:

// User.php
<?php

class Post extends Eloquent
{
    public function save()
    {
        // code before save
        parent::save(); 
        //code after save
    }
}

When I try and run a this method in my unit testing I get the following error:

..{"error":{"type":"ErrorException","message":"Declaration of Post::save() should be compatible with that of Illuminate\\Database\\Eloquent\\Model::save()","file":"\/var\/www\/laravel\/app\/models\/Post.php","line":4}}
like image 604
wkm Avatar asked Aug 13 '13 02:08

wkm


3 Answers

Create Model.php class which you will extend in another 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 for 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 your Post model.
Also, you need to define validation rules for this model.

app/models/Post.php

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

Controller method
Thanks to Model class, Post model is automaticaly validated on every call to 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 is strongly based on Jeffrey Way's Laravel Model Validation package for Laravel 4.
All credits to this man!

like image 183
Andreyco Avatar answered Nov 11 '22 14:11

Andreyco


How to override Model::save() in Laravel 4.1

public function save(array $options = array())
{
   parent::save($options);
}
like image 40
th3d0g Avatar answered Nov 11 '22 12:11

th3d0g


If you want to overwrite the save() method, it must be identical to the save() method in Model:

<?php
public function save(array $options = array()) {}

And; you can also hook in the save() call with the Model Events: http://laravel.com/docs/eloquent#model-events

like image 24
Rob Gordijn Avatar answered Nov 11 '22 12:11

Rob Gordijn