routes.php:
Route::get('users/{id}/edit', array('before' => 'auth|admin', 'uses' => 'UserController@editUser'));
Route::post('users/{id}/edit', array('before' => 'auth|admin', 'uses' => 'UserController@editUserPost'));
UserController.php:
public function editUser($id) {
$user = $this->user->find($id);
return View::make('auth.editUser')->with('user', $user);
}
public function editUserPost() {
// Validation
$validator = new UserEditFormValidator(Input::all());
if ($validator->fails()) {
return Redirect::action('UserController@editUser')->withErrors($validator->errors()); // THIS BUGGED
}
$input = array(
'id' => Input::get('id'),
'name' => Input::get('name'),
'active' => (Input::has('active')) ? true : false,
'password' => Input::get('password')
);
$this->user->update($input);
return Redirect::action('UserController@userList')->with('success', 'Your profile has been updated');
}
editUser.blade.php:
{{ Form::open(array('url' => (Request::is('users/*')) ? 'users/' . $user->id . '/edit' : 'my/edit')) }}
{{ Form::hidden('id', $user->id) }}
<div class="form-group">
{{ Form::label('name', 'Your name') }}
{{ Form::text('name', $user->name, array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password', 'Your password') }}
{{ Form::password('password', array('class' => 'form-control')) }}
</div>
<div class="form-group">
{{ Form::label('password_confirmation', 'Your password one more time') }}
{{ Form::password('password_confirmation', array('class' => 'form-control')) }}
</div>
{{ Form::submit('Save', array('class' => 'btn btn-default')) }}
{{ Form::close() }}
I am getting the error "Trying to get property of non object" when my validate fails in UserController.php Can't understand why it is happening, need a help..
return Redirect::action('UserController@editUser')->withErrors($validator->errors()); // THIS BUGGED
Does this need you to pass the ID parameter like so:
return Redirect::action('UserController@editUser' , array('id'=>Auth::id()) )->withErrors($validator->errors());
Thanks, i will try it out. But can i pass not my current ID, but the id of user which i am looking for. In my case it's 1. With Auth::id() i am getting my current id.
Ahh of course yes, pass the id of the post owner :)
Thanks, but why do i have to do that? I have other method in same controller, but it works without sending any variables:
public function editMyProfile() {
$user = $this->user->find(Auth::id());
return View::make('auth.editUser')->with('user', $user);
}
public function doEditMyProfile() {
// Validation
$validator = new UserEditFormValidator(Input::all());
if ($validator->fails()) {
return Redirect::action('UserController@editMyProfile')->withErrors($validator->errors());
}
$input = array(
'id' => Input::get('id'),
'name' => Input::get('name'),
'active' => (Input::has('active')) ? true : false,
'password' => Input::get('password')
);
$this->user->update($input);
return Redirect::action('UserController@editMyProfile')->with('success', 'Your profile data has been updated');
}
I have noticed, when i submit form via POST it redirect me to url:
users/{id}/edit
instead of:
users/1/edit
So i need to send variable with Redirect class only in case of using get variables?
I beleive its because your method editUser requires the parameter $id
public function editUser($id) {
$user = $this->user->find($id);
return View::make('auth.editUser')->with('user', $user);
}
Where as editMyProfile doesn't
public function editMyProfile() {
$user = $this->user->find(Auth::id());
return View::make('auth.editUser')->with('user', $user);
}
At least that'd be my assumptions
Hope that helps
heihachi88 said:
Thanks, i will try it out. But can i pass not my current ID, but the id of user which i am looking for. In my case it's 1. With Auth::id() i am getting my current id.
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community