Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

Chimpoo's avatar

$errors variable not available in controller and blade

So I am making a project on Laravel 9 and in a simple login form I added validation rules in the controller and try to show it using $errors variable in blade using @if($errors->any())

@endif but no error was showing up there on the page. Then I try to access $errors variable in controller but VsCode shows red curly line underneath the $errors variable . Documentation tells that it is there by default but it looks like $errors variable is not availabe in my controller and thats why not being sent to the blade file. Is this some general issue in larave9 ? How can I fix it?

0 likes
17 replies
MohamedTammam's avatar

If you don't see an error, that's means there's no any validation error.

VSCode shows red line because it doesn't recognize it, however as the documentation mentioned it's there in the view by default

MohamedTammam's avatar

@Chimpoo How are you validating? can you please show the code both the view file and the controller.

Chimpoo's avatar

@MohamedTammam In Controller public function emplogin(request $req){

     $validate = $req->validate([
        'email' => 'required',
      'password' => 'required|min:5',
         ]);
        return $req->input();

}

in Blade file

@csrf @if($errors->any()) @endif
        <!-- Email input -->
        <div class="form-outline mb-4">
          <input type="email" id="form3Example3" class="form-control form-control-lg"
            placeholder="Enter a valid email address" name="email" />
          <label class="form-label" for="form3Example3">Email address</label>
        </div>

        <!-- Password input -->
        <div class="form-outline mb-3">
          <input type="password" id="form3Example4" class="form-control form-control-lg"
            placeholder="Enter password" name="password" />
    //      ..... more code below
Chimpoo's avatar

@MohamedTammam Also I tried making a new project with the same code but in that new project in he kernel.php file I dont have \Illuminate\Session\Middleware\StartSession::class, in the global HTTP middleware stack. so I commented out this line in my original project having this errors and erros starts showing up but I have adminlogin code also on this project. After commentng out this line, that login page is now giving errors ike "Session store not set on request."

Chimpoo's avatar

@MohamedTammam Its there Heres te whole code

Sample image
    <div class="col-md-8 col-lg-6 col-xl-4 offset-xl-1">
      <form action="" method="post">
          @csrf  
          @if($errors->any())
          <div>
            <ul>
                @foreach ($errors->all() as $error)
                    <li>{{ $error }}</li>
                @endforeach
            </ul>
        </div>
        @endif
    
        <!-- Email input -->
        <div class="form-outline mb-4">
          <input type="email" id="form3Example3" class="form-control form-control-lg"
            placeholder="Enter a valid email address" name="email" />
          <label class="form-label" for="form3Example3">Email address</label>
        </div>

        <!-- Password input -->
        <div class="form-outline mb-3">
          <input type="password" id="form3Example4" class="form-control form-control-lg"
            placeholder="Enter password" name="password" />
          
          <label class="form-label" for="form3Example4">Password</label>
        </div>
        {{-- <div>
          @error('password')
          <span>{{$message}}</span>
       @enderror
        </div> --}}

        <div class="d-flex justify-content-between align-items-center">
          <!-- Checkbox -->
        
          <a href="#!" class="text-body">Forgot password?</a>
        </div>

        <div class="text-center text-lg-start mt-4 pt-2">
          <button type="submit" class="btn btn-primary btn-lg"
            style="padding-left: 2.5rem; padding-right: 2.5rem;">Login</button>
          <p class="small fw-bold mt-2 pt-1 mb-0">Don't have an account? <a href="{{route('reg')}}"
              class="link-danger">Register</a></p>
        </div>

      </form>
    </div>

  </div>
</div>
Chimpoo's avatar

@MohamedTammam I am using the home page with routes like

Route::get('/', [Emp_Controller::class,'emp_welcome']);

Route::post('/', [Emp_Controller::class,'emplogin']);

and in the controller

public function emp_welcome(){ return view('emp_login'); }

public function emplogin(request $req){
    
     $validate = $req->validate([
        'email' => 'required',
      'password' => 'required|min:5',
         ]);
        return $req->input();

}

Chimpoo's avatar

It seems like \Illuminate\Session\Middleware\StartSession::class, in gobal mddleware list (which runs during every request to your application) is making this issue. After commenting out this line, The errors are showing up but then I cant make login logic and start any session

Chimpoo's avatar

upate I cmmented out \Illuminate\Session\Middleware\StartSession::class, from 'web' in group midlewares and now everyting is fine

Chimpoo's avatar

@MohamedTammam the one I commented out(\Illuminate\Session\Middleware\StartSession::class) is already there in gobal midleware list also which runs on every request. Is it still fine or not?

Snapey's avatar

please read the docs and let laravel redirect after validation, then all will be good

You don't have $errors in your controller, the framework ensures it is created when rendering a view

Please or to participate in this conversation.