I have many different locations ways of inserting and updating my database and I would like to be able to trim() the users input before inserting into the database. I know in the model I can do something like below, but I do not want to do this for every field. Is there a way to set a  generic setter that works on all fields?
Example:
public function setSomFieldAttribute($value) {
     return $this->attributes['some_field'] = trim($value);
}
                You probably will be able to override those methods:
<?php
class Post extends Eloquent {
    protected function getAttributeValue($key)
    {
        $value = parent::getAttributeValue($key);
        return is_string($value) ? trim($value) : $value;
    }
    public function setAttribute($key, $value)
    {
       parent::setAttribute($key, $value);
        if (is_string($value))
        {
            $this->attributes[$key] = trim($value);
        }
    }
}
And you should never get an untrimmed value again.
EDIT:
Tested this here and I got no spaces:
Route::any('test', ['as' => 'test', function()
{
    $d = Post::find(2);
    $d->title_en = "  Markdown Example  ";
    dd($d);
}]);
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With