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}}
If you want to overwrite the save() method, it must be identical to the save() method in Model:
And; you can also hook in the save() call with the Model Events: http://laravel.com/docs/eloquent#model-events
Create Model.php class which you will extend in another self-validating models
app/models/Model.php
Then, adjust your Post model.
Also, you need to define validation rules for this model.
app/models/Post.php
Controller method
Thanks to Model class, Post model is automaticaly validated on every call to
save()
methodThis answer is strongly based on Jeffrey Way's Laravel Model Validation package for Laravel 4.
All credits to this man!
How to override
Model::save()
in Laravel 4.1