It had been many months since I'm using laravel but never faced such problem.
I have made a simple Request class to validate the the update user request which works fine if validation rules are followed. If validation rule fails we should come back to the previous page and display all errors in html.
According to me I have written everything correctly as I used to write in other applications but the $errors
seems to be inaccessible in blade
Following are my required code snippets to debug:
routes.php
Route::group(['middleware' => ['web']], function () {
Route::get('/users/{id}/edit', 'UserController@edit');
Route::post('/users/{id}/edit', 'UserController@update');
});
UserController.php
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Http\Requests;
use App\Http\Requests\UserUpdateRequest;
use App\Models\User;
use App\Models\Role;
use App\Models\Post;
class UserController extends Controller
{
public function edit($id)
{
try {
$user = User::find($id);
$roles = Role::all();
return view('users.edit', compact(['user', 'roles']));
}catch(Exception $e) {
return view('errors.500', compact(['e']));
}
}
public function update($id, UserUpdateRequest $request)
{
dd($request);
}
}
UserUpdateRequest.php
<?php
namespace App\Http\Requests;
use App\Http\Requests\Request;
class UserUpdateRequest extends Request
{
/**
* Determine if the user is authorized to make this request.
*
* @return bool
*/
public function authorize()
{
return true;
}
/**
* Get the validation rules that apply to the request.
*
* @return array
*/
public function rules()
{
return [
'name' => 'required|string|min:4',
'email' => 'required|email',
'role' => 'required|numeric',
'password' => 'required',
];
}
}
edit.blade.php
@extends('master')
@section('title') Edit Users @stop
@section('content')
<div class="row">
<div class="col-sm-12">
<h2>Edit User</h2>
</div>
</div>
<div class="alert alert-warning alert-dismissible" role="alert">
@foreach($errors->all() as $error)
{{ $error }}
@endforeach
</div>
<form action="{{ url('/users/'.$user->id.'/edit') }}" method="post">
<input type="hidden" name="_token" value="{{ csrf_token() }}">
<div class="col-sm-6">
<div class="form-group">
<label>Name</label>
<input type="text" name="name" value="{{ $user->name }}" class="form-control" placeholder="Name">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Email Address</label>
<input type="text" name="email" value="{{ $user->email }}" class="form-control" placeholder="Email Address">
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Role</label>
<select name="role" class="form-control">
@foreach($roles as $role)
@if($role->id == $user->role)
<option value="{{ $role->id }}" selected>{{ $role->name }}</option>
@else
<option value="{{ $role->id }}">{{ $role->name }}</option>
@endif
@endforeach
</select>
</div>
</div>
<div class="col-sm-6">
<div class="form-group">
<label>Password</label>
<input type="password" name="password" class="form-control" placeholder="New Password">
</div>
</div>
<div class="col-sm-12">
<div class="form-group">
<input type="submit" class="btn btn-info btn-block" value="Update">
</div>
</div>
</form>
@stop
The HTML response on browser is blank.
I also tried <?php dd($errors); ?>
which displayed the following
Edit User
ViewErrorBag {#168 ▼
#bags: []
}
More info here
storage/logs/laravel.log
– Cybersupernova