php - Patch update Authenticated User in Laravel -
i'm trying edit/update profile information users table.
my idea authenticated user able edit own profile in users table.
during registration, required fill out couple specific items (username, name, lastname, email, password) i've added couple columns user table (city, country, phone, twitter, facebook).
i have profile user page (route= '/profile') information shown. of course columns aren't required during registration not filled in:
i have edit page columns need info added editable:
here code editprofile.blade.php (where try send out 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 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 http/requests/updateuserrequest.php validate 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; } /** * validation rules apply request. * * @return array */ public function rules() { return [ 'city' => 'max:30', 'country' => 'max:30', 'phone' => 'max:30', 'twitter' => 'max:30', 'facebook' => 'max:30' ]; } }
and http/controllers/profilecontroller.php 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 moment seems can't open 'editprofile.blade.php' page anymore, unless remove 'route' & 'methode' form. keep getting error:
could guide me on how should trigger patch methode?
change form opening tag to
{!! form::model($user, ['route' => 'user/' . $user->id , 'method' => 'patch']) !!}
you forgot '->id'
update
change route from
route::patch('user/{user}', 'profilecontroller@update');
to
route::patch('user/{user}', 'as' => 'profile.patch', 'profilecontroller@update');
and form opening tag to
{!! form::model($user, ['route' => array('profile.patch', $user->id), 'method' => 'patch']) !!}
Comments
Post a Comment