4

I have a simple Model IsolatedQuery which consists of a name and query field. I have defined those two fields in the $fillable property of the model. The IsolatedQueryController@store looks like this:

public function store(IsolatedQueryRequest $request)
{
    IsolatedQuery::insert($request->all());

    session()->flash('flash_message', 'Isolated Query succesvol opgeslagen');

    return redirect('iq');
}

For completeness, here is the Model's source (it is as little as I described it)

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class IsolatedQuery extends Model
{
    protected $fillable = [
        'name',
        'query'
    ];
}

The IsolatedQueryRequest only requires both name and query to be filled with any value.

When calling the store method with a given name and query value I get the error: SQLSTATE[42S22]: Column not found: 1054 Unknown column '_token' in 'field list'.

It's obvious a _token field gets send with the request but I'm a bit baffled why it's trying to store it with the actual SQL query as it's not listed in the $fillable array.

Why is it getting mass assigned?

Ben Fransen
  • 10,884
  • 18
  • 76
  • 129
  • Possible duplicate of [Laravel 5.2 $fillable](http://stackoverflow.com/questions/34864666/laravel-5-2-fillable) – bishop Apr 14 '16 at 12:52
  • @bishop It's not a duplicate, this question asks why it's happening and the one you link to doesn't answer that. Also, this question is about using the fillable proprty, and the other one is about not using it. – Joel Hinz Apr 14 '16 at 12:53
  • It's not a duplicate indeed. I want to apply `$request->all()` is it should be possible, rather than calling `$request->only`. If I wanted that I wouldn't have to defined the mass assignable list with `$fillable`. – Ben Fransen Apr 14 '16 at 12:54

1 Answers1

4

You use Query Builder's method insert. It doesn't check the fillable.
You should use the create or update method of Eloquent.
Please, read the documentation.

Also, you may pass input data to construct or fill method of Eloquent. After that you may use the save method.

Vitaliy Kravchyshyn
  • 198
  • 1
  • 2
  • 11
  • Howly mowly.. I've completely looked over it... Thanks! +1A – Ben Fransen Apr 14 '16 at 12:55
  • One may also use [`Eloquent::fill()`](https://github.com/laravel/framework/tree/master/src/Illuminate/Database/Eloquent/Model.php#L428-L454), though IMO `create` and `update` express the intent more clearly. – bishop Apr 14 '16 at 13:01
  • so what you will do when you have a large data to insert it once? looping through it and use create method is not efficient at all – wahdan Jan 11 '18 at 05:42