2 years ago

Route Model Binding and Fillable in a [email protected] method

Posted 2 years ago by FrenchFryNinja

I don't know what I'm doing wrong here. I have a form that submits a patch request to the route and the route given as follows:

Route::patch('patients/{patient}', '[email protected]');

and update method in the controller:

    public function update(Request $request, Patient $patient)
        return redirect()->action('[email protected]');

dd on request() or $request gives me exactly what I want:

array:12 [▼
  "_method" => "PATCH"
  "_token" => "2KWffXiRGEuBxOdAmfuL9EbBaX4nFilAAJFZhpo4"
  "first_name" => "Wendell"
  "middle_name" => "THE THING I'M CHANGING!"
  "last_name" => "Abernathy"
  "address1" => "846 Waters Plain"
  "address2" => null
  "address3" => null
  "city" => "North Amber"
  "state" => "TO"
  "postal_code" => "78951"
  "email" => null

This does not work so I have to manually update every field I want to in body of the update function:

        $patient->middle_name = request()->middle_name;
        $patient->last_name = \request()->last_name;
 .... etc.

Is there a faster way of doing this where I can reference the fillables somehow? I wrapped the protected $fillables with a getter and I'm iterating over the request object for updates like this and that works, but this seems like with all of the other laravel tools that I'm missing something here that should be done for me.

Here's what I'm doing that works:

    public function update(Patient $patient)
        foreach(request()->all() as $key => $value)
            if(in_array($key, $patient->getFillables())){
                $patient->$key = $value;
        return redirect()->action('[email protected]');

Please sign in or create an account to participate in this conversation.