Cedric Bongaerts Cedric Bongaerts - 3 months ago 14
PHP Question

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 columns to the User table (city, country, phone, twitter, facebook).

I have a profile user page (route= '/profile') where all the information is shown. Of course all columns that aren't required during the registration are not filled in:

Profile Page

I also have an edit page where the columns that need info added are editable:

Profile Edit Page

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

...

{!! 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', 'PagesController@profile');
Route::get('/profile/edit', 'ProfileController@edit');


Route::bind('user', function($id) {
$user = App\User::find($id)->first();
});
Route::patch('user/{user}', 'ProfileController@update');


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:

Route/Methode Error

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

Answer

change your form opening tag to

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

you forgot '->id'

UPDATE

Change you Route from

Route::patch('user/{user}', 'ProfileController@update');

to

Route::patch('user/{user}', 'as' => 'profile.patch', 'ProfileController@update');

and your form opening tag to

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