Ok! Lemme what does below code for you?
public function update(UserRequest $request, User $user)
{
print('<pre style="color:red;">');
print_r($request->all());
print('</pre>');
exit;
$user->update($request->all());
return redirect('admin/users')->with('success', 'Updated Successfully');
}
Thanks for your fast reply. I tried your code but that's the problem it doesn't return anything it just refreshes the page.
I think the problem is in the UserRequest.php because if I just use the Regular (Request $request) it works fine but then I can't validate the password or email.
Let me be a little clear, the Admin can change anyone's passwords (that's the goal if I get this to work). Do I have to pass the ID to the UserRequest.php of the User the admin is trying to change the password for?
Provide your route(app/http/routes.php
) and middleware(app/Http/Kernel.php
) code.
Your page might be refreshing be coz the validation is failing ? is the show validation template there in your view ? you need to add this somewhere in the view
@if (count($errors) > 0)
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
Im on Laravel 5.3 so here is the web.php
// Administrator Section Routes
Route::group(['middleware'=>'admin'], function () {
Route::get('/admin', function() {
return view('admin.index');
});
Route::resource('/admin/posts', 'Admin\PostsController');
Route::resource('/admin/teams', 'Admin\TeamsController');
Route::resource('/admin/athletes', 'Admin\AthletesController');
Route::resource('/admin/users', 'Admin\UserController');
});
Here is the Kernel.php
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'admin' => \App\Http\Middleware\Admistrator::class,
];
}
I'm certain that when updating I need to change the rules for validation. I removed the 'password' and 'email' rules and It updates properly, without those 2 rules.
@astroanu I do have that in my layout view I usually get errors when something is wrong. But for this it just refreshes without telling me what's wrong.
@iAmXquisite Lemme what does below code(Kernel.php) for you:
class Kernel extends HttpKernel
{
/**
* The application's global HTTP middleware stack.
*
* These middleware are run during every request to your application.
*
* @var array
*/
protected $middleware = [
\Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class,
\Illuminate\Session\Middleware\StartSession::class,
\Illuminate\View\Middleware\ShareErrorsFromSession::class,
];
/**
* The application's route middleware groups.
*
* @var array
*/
protected $middlewareGroups = [
'web' => [
\App\Http\Middleware\EncryptCookies::class,
\Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
\App\Http\Middleware\VerifyCsrfToken::class,
\Illuminate\Routing\Middleware\SubstituteBindings::class,
],
'api' => [
'throttle:60,1',
'bindings',
],
];
/**
* The application's route middleware.
*
* These middleware may be assigned to groups or used individually.
*
* @var array
*/
protected $routeMiddleware = [
'auth' => \Illuminate\Auth\Middleware\Authenticate::class,
'auth.basic' => \Illuminate\Auth\Middleware\AuthenticateWithBasicAuth::class,
'bindings' => \Illuminate\Routing\Middleware\SubstituteBindings::class,
'can' => \Illuminate\Auth\Middleware\Authorize::class,
'guest' => \App\Http\Middleware\RedirectIfAuthenticated::class,
'throttle' => \Illuminate\Routing\Middleware\ThrottleRequests::class,
'admin' => \App\Http\Middleware\Admistrator::class,
];
}
@AddWebSolution Thank for your help but I found the solution it was in my UserRequest.php file.
So basically if you're updating a user that already exists you need to put the user ID at the end so it can know that you're just updating the user and not trying to add a new user with same email address.
'email' => 'required|email|unique:users,email,' . $this->user->id
Since I'm using the same UserRequest.php for Creating and Editing Users I just made sure they were received the correct Rules
/**
* Get the validation rules that apply to the request.
* Checks if user is creating or editing user.
*
* @return array
*/
public function rules()
{
if (Request::isMethod('PATCH'))
{
$emailRule = 'required|email|unique:users,email,' . $this->user->id;
$passwordRule = Request::get('password') != '' ? 'required|min:6|confirmed' : '';
}
else
{
$emailRule = 'required|email|unique:users,email';
$passwordRule = 'required|min:6|confirmed';
}
return [
'name' => 'required|max:255',
'email' => $emailRule,
'password' => $passwordRule,
];
}
Sign in to participate in this thread!
The Laravel portal for problem solving, knowledge sharing and community building.
The community