Laravel 5.2 $fillable
Let's say, my model has the following DB structure:
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
There is a cleaner solution using
This solution is also better if you are using a different
$connection for the model, which was my case.
Instead of using
all(), you can use the
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']));
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())));
- → "failed to open stream" error when executing "migrate:make"
- → October CMS Plugin Routes.php not registering
- → OctoberCMS Migrate Table
- → OctoberCMS Rain User plugin not working or redirecting
- → October CMS Custom Mail Layout
- → October CMS - How to correctly route
- → October CMS create a multi select Form field
- → October CMS - Conditionally Load a Different Page
- → How to disable assets combining on development in OctoberCMS
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → OctoberCms component: How to display all ID(items) instead of sorting only one ID?
- → In OctoberCMS how do you find the hint path?
- → How to register middlewares in OctoberCMS plugin?