Ad

Laravel 5.2 $fillable

Let's say, my model has the following DB structure:

name:string
last_name:string

And I wish to update the model, with this $request->input() array given:

name => 'Quinn',
last_name => 'Daley',
middle_name => 'Phill'

I would get an error message, because it would try to update the middle_name field, which does not exist in the DB.

So here is my question: How can I make it so only existing fields are updated? Without using the protected $fillable property.

Edit: the reason I don't want to use $fillable is that I don't want to list all the existing fields from the database. What if I have a large model with 50 fields in it? Isn't there a possibility to only accept parameters that exist in the model DB from the $request->input()?

UPDATE

There is a cleaner solution using $model->getAttributes():

$user->update($request->only(array_keys($user->getAttributes()))

This solution is also better if you are using a different $connection for the model, which was my case.

Ad

Answer

Instead of using input() or all(), you can use the only() or except() methods on the request to limit the fields. For example:

$user = User::find(1);

$user->update($request->only(['name', 'last_name']));
// or
$user->update($request->except(['middle_name']));

Edit

If you want to get the column names of the table, you can do this using Schema::getColumnListing($tableName);. So, in this case, your code would look something like:

$user = User::find(1);

$user->update($request->only(Schema::getColumnListing($user->getTable())));
Ad
source: stackoverflow.com
Ad