Patch update Authenticated User in Laravel

I'm trying to edit/update profile information into my Users table. My idea is for the authenticated user to be able to edit his own profile in the Users table. During the registration, you are only required to fill out a couple specific items (username, name, lastname, email, password) but I've also added a couple extra coloms to the User table (city, country, phone, twitter, facebook).

I have a profile user page (route= '/profile') where all the information is shown. Ofcourse all coloms that aren't required during the registration are not filled in: Patch update Authenticated User in Laravel

I also have an edit page where the coloms that need info added are editable: Patch update Authenticated User in Laravel

Here is the code for this editprofile.blade.php (where I try to send out a PATCH methode):

... {!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!} <div class="form-group form-horizontal"> <div class="form-group"> {!! Form::label('username', 'Username:', ['class' => 'col-md-4 control-label']) !!} <div class="col-md-6"> <label class="align-left">{{ $user->username}}<label> </div> </div> <div class="form-group"> {!! Form::label('email', 'E-mail:', ['class' => 'col-md-4 control-label']) !!} <div class="col-md-6"> <label class="align-left">{{ $user->email}}<label> </div> </div> <div class="form-group"> {!! Form::label('name', 'Name:', ['class' => 'col-md-4 control-label']) !!} <div class="col-md-6"> <label class="align-left">{{ $user->name}} {{ $user->lastname}}<p> </div> </div> <br /> <div class="form-group"> {!! Form::label('city', 'City:', ['class' => 'col-md-4 control-label']) !!} <div class="col-md-6"> {!! Form::Text('city', null, ['class' => 'form-control']) !!} </div> </div> <div class="form-group"> {!! Form::label('country', 'Country:', ['class' => 'col-md-4 control-label']) !!} <div class="col-md-6"> {!! Form::Text('country', null, ['class' => 'form-control']) !!} </div> </div> <div class="form-group"> {!! Form::label('phone', 'Phone:', ['class' => 'col-md-4 control-label']) !!} <div class="col-md-6"> {!! Form::Text('phone', null, ['class' => 'form-control']) !!} </div> </div> <div class="form-group"> {!! Form::label('twitter', 'Twitter link:', ['class' => 'col-md-4 control-label']) !!} <div class="col-md-6"> {!! Form::Text('twitter', null, ['class' => 'form-control']) !!} </div> </div> <div class="form-group"> {!! Form::label('facebook', 'Facebook link:', ['class' => 'col-md-4 control-label']) !!} <div class="col-md-6"> {!! Form::Text('facebook', null, ['class' => 'form-control']) !!} </div> </div> <div class="form-group"> <div class="col-md-6 col-md-offset-4"> {!! Form::submit('Save Profile', ['class' => 'btn btn-primary']) !!} </div> </div> </div> </div> {!! Form::close() !!} ...

I have this is my Http/routes.php:

# Profile Route::get('/profile', '[email protected]'); Route::get('/profile/edit', '[email protected]'); Route::bind('user', function($id) { $user = App\User::find($id)->first(); }); Route::patch('user/{user}', '[email protected]');

I have an Http/Requests/UpdateUserRequest.php to validate the request:

<?php namespace App\Http\Requests; use App\Http\Requests\Request; use Illuminate\Foundation\Http\FormRequest; class UpdateUserRequest extends Request { public function authorize() { return false; } /** * Get the validation rules that apply to the request. * * @return array */ public function rules() { return [ 'city' => 'max:30', 'country' => 'max:30', 'phone' => 'max:30', 'twitter' => 'max:30', 'facebook' => 'max:30' ]; } }

And my Http/Controllers/ProfileController.php with the update fuction:

<?php namespace App\Http\Controllers; use Auth; use App\Http\Requests; use App\Http\Controllers\Controller; use Illuminate\Http\Request; class ProfileController extends Controller { public function edit() { $user = Auth::user(); return view('pages.editprofile')->withUser($user); } public function update(UpdateUserRequest $request, User $user) { $user->fill($request->all()); $user->save(); return redirect()->view('pages.editprofile')->withUser($user); } }

At the moment it seems I can't even open my 'editprofile.blade.php' page anymore, unless I remove the 'route' & 'methode' from my form. I keep getting this error:

Patch update Authenticated User in Laravel

Could anyone guide me on how I should exactly trigger the PATCH methode?


change your form opening tag to

{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}

you forgot '->id'


Change you Route from

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


Route::patch('user/{user}', 'as' => 'profile.patch', '[email protected]');

and your form opening tag to

{!! Form::model($user, ['route' => array('profile.patch', $user->id), 'method' => 'PATCH']) !!}

You need to change:

{!! Form::model($user, ['route' => 'user/' . $user , 'method' => 'PATCH']) !!}


{!! Form::model($user, ['route' => 'user/' . $user->id , 'method' => 'PATCH']) !!}

At the moment you create URL in your form with User object converted to Json - that's why it doesn't work.

Category:php Time:2018-11-05 Views:0

Related post

Copyright (C), All Rights Reserved.

processed in 0.598 (s). 13 q(s)