Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent - $fillable is not working?

I have set the variable $fillable in my model. I wanted to test the update functionality, and I get this error:

SQLSTATE[42S22]: Column not found: 1054 Unknown column '_method' in 'field list' (SQL: update positions set name = Casual Aquatic Leader, _method = PUT, id = 2, description = Here is my description, updated_at = 2014-05-29 17:05:11 where positions.client_id = 1 and id = 2)"

Why is this yelling at _method when my fillable doesn't have that as a parameter? My update function is:

Client::find($client_id)
        ->positions()
        ->whereId($id)
        ->update(Input::all());
like image 333
Kousha Avatar asked May 30 '14 00:05

Kousha


1 Answers

Change following:

->update(Input::all());

to this (exclude the _method from the array)

->update(Input::except('_method'));

Update:

Actually following update method is being called from Illuminate\Database\Eloquent\Builder class which is being triggered by _call method of Illuminate\Database\Eloquent\Relations class (because you are calling the update on a relation) and hence the $fillable check is not getting performed and you may use Input::except('_method') as I answered:

public function update(array $values)
{
    return $this->query->update($this->addUpdatedAtColumn($values));
}

If you directly call this on a Model (Not on a relation):

Positions::find($id)->update(Input::all());

Then this will not happen because fillable check will be performed within Model.php because following update method will be called from Illuminate\Database\Eloquent\Model class:

public function update(array $attributes = array())
{
    if ( ! $this->exists)
    {
        return $this->newQuery()->update($attributes);
    }

    return $this->fill($attributes)->save();
}
like image 86
The Alpha Avatar answered Oct 21 '22 17:10

The Alpha