5

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());
Kousha
  • 32,871
  • 51
  • 172
  • 296

4 Answers4

8

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();
}
The Alpha
  • 143,660
  • 29
  • 287
  • 307
0

write a parent class

class BaseModel extends Model

public static function getFillableAttribute(Model $model, $data){
    $array = $model->getFillable();
    $arr = [];
    foreach ($array as $item){
        if( isset($data["$item"])){
            $arr["$item"] = $data["$item"];
        }
    }
    return $arr;
}
0

I experienced this breaking after updating from Laravel 5.2 to 5.4 - I can't find anything in the documentation / migration guide that covers it.

As covered in this github issue the correct fix/use of Eloquent seems to be:

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

To trigger the fillable check by laravel and then perist the changes.

nover
  • 2,259
  • 1
  • 27
  • 27
0

You could also do this

$data = request()->all();
//remove them from the array
unset($data['_token'],$data['_method']);
//then
Client::find($client_id)
        ->positions()
        ->whereId($id)
        ->update($data);

This removes the _method and _token from the array

ManuEl Magak
  • 317
  • 3
  • 11