0

I was wondering if it was possible to update/save a model with the validator data? I've tried googling and can't seem to find anything.

I'm just trying to do something like this

    $validator = Validator::make(request()->all(), [
        'first_name' => ['required', 'string', 'max:255'],
        'last_name' => ['required', 'string', 'max:255'],
        'address_line_1' => ['required', 'string', 'max:255'],
        'address_line_2' => ['required', 'string', 'max:255'],
        'postcode' => ['required', 'string', 'max:255'],
        'city' => ['required', 'string', 'max:255'],
        'county' => ['required', 'string', 'max:255'],
    ]);

    $address = $validator->getData();
    $address->save();

rather then having to set all the variables one by one and then saving?

adKB
  • 27
  • 4

2 Answers2

3

Since getData() returns an associative array, you can use the update method:

$validator = Validator::make(request()->all(), [
    'first_name' => ['required', 'string', 'max:255'],
    'last_name' => ['required', 'string', 'max:255'],
    'address_line_1' => ['required', 'string', 'max:255'],
    'address_line_2' => ['required', 'string', 'max:255'],
    'postcode' => ['required', 'string', 'max:255'],
    'city' => ['required', 'string', 'max:255'],
    'county' => ['required', 'string', 'max:255'],
]);

$address->update($validator->getData());

Or (in my opinion), you can make use od the validate method of request that returns the validated data (if validation is successful)

$address->update(request()->validate([
    'first_name' => ['required', 'string', 'max:255'],
    'last_name' => ['required', 'string', 'max:255'],
    'address_line_1' => ['required', 'string', 'max:255'],
    'address_line_2' => ['required', 'string', 'max:255'],
    'postcode' => ['required', 'string', 'max:255'],
    'city' => ['required', 'string', 'max:255'],
    'county' => ['required', 'string', 'max:255'],
]));

Be aware that (depending on which version of Laravel are you using) in order to perform those types of operations, you might have to disable guard for those fields in the Model:

class ... extends Model{
     protected $guarded = [];
     // or
     protected $fillable = [/*list of attributes*/]
}
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
2

You can do this if you want

Create

$address = Address::create($request->all());

Update

$address = Address::update($request->all());

This will do your job as you wanted to.

But you need to be careful about the mass assignment(don't forget to read the NOTE below)

If you perform mass assignment(the solution that I wrote above), you shouldn't take any risks. Therefore, in your model set fillable or guarded property accordingly.

You can read about it in detail over here. https://laravel.com/docs/7.x/eloquent#mass-assignment

NOTE: But the fillable check doesn't work with the update in a few scenarios.

It will perform the fillable check

Model::update([..]);

It won't perform the check in this case.

Model::where('attribute', 'some value')->update([..]);

These references have a detailed explanation to this behavior.

https://github.com/laravel/framework/issues/16447

Laravel Eloquent - $fillable is not working?

Aashish gaba
  • 1,726
  • 1
  • 5
  • 14
  • 1
    Yes, you can do the same with the update as well. But when update is called, it doesn't perform the fillable check. Here's the reason why. https://stackoverflow.com/questions/23945040/laravel-eloquent-fillable-is-not-working – Aashish gaba Jul 30 '20 at 12:31