Q: How to make conditional constructor statement within a controller in laravel

I have a controller which uses two models of authenticatable type of users, if i’m authenticated as an applicant it successfully renders the applicant.index view, and the same thing goes to if I’m authenticated as an Employer. The issue for me is when I’m not logged in and visits the /app it renders empty page.

How can protect this route and redirect to for example '/' route.

A controller that indexes two models with two different views:

class ProfilesController extends Controller

    public function index()

    if (auth('applicant')->check())
        $applicants = Applicant::where('id', '!=', Auth::guard('applicant')->user()->id)->get();
        return view('applicant.index', compact('applicants'));

    } elseif (auth('employer')->check()) {
        $employers = Employer::where('id', '!=', Auth::guard('employer')->user()->id)->get();
        return view('employer.index', compact('employers'));


using this following route:

Route::get('/app/', '[email protected]');

I have tried this piece of code but it’s not working :

public function __construct()
        if (auth('applicant')->check())
        } elseif  (auth('employer')->check()) {
        } else {
           return abort(404);


the else statement takes over even if i’m authenticated and returns 404.

Stack Overflow Asked by seddka on January 5, 2022

1 Answers

One Answer

The constructor of the Controller is hit before the Request is passed through the middleware stack (This is how Laravel can actually get the middleware you assign in the constructor for the Controller). This means the session has not started yet so you won't have access to auth or the session at that point.

The auth middleware itself takes a variable amount of parameters as guards; you can pass it multiple guards for it to check:


This will spin through all those guards and if one of them returns a User it will set that guard as the default and let the Request pass through. If it spins through the guards and none of them authenticate then the Request is not passed through (auth failed).

Answered by lagbox on January 5, 2022

Add your own answers!

Related Questions

How to delete entries from a SELECT query result in mysql?

3  Asked on November 12, 2021 by optimus-servers


How to convert a list made up of one tuple into a two item list?

4  Asked on November 12, 2021 by python-learner-93


How to remove box and whiskers from plot() function in R?

1  Asked on November 12, 2021 by david_authors_books


Trying to pass list value from python to html

1  Asked on November 12, 2021 by goodaytar


How do I set the buffer value to zero?

1  Asked on November 12, 2021 by kingwales


Removing part of DirectoryName

2  Asked on November 12, 2021


Javascript changes value of three objects at once

1  Asked on November 12, 2021 by oskarmular


Rubyist way to test for a hash key in an array

2  Asked on November 12, 2021 by lucky


How do I rotate an object 90 degrees using JQuery

3  Asked on November 12, 2021 by bullybear17


Add a year to current date

2  Asked on November 12, 2021 by user13788524


Python exact time usage

1  Asked on November 12, 2021 by teac


Array of dates prints unexpected dates in moment js

3  Asked on November 12, 2021 by jayakrishnan


Is this syntax – [x] = ‘y’ in an array literal – standard C?

1  Asked on November 12, 2021 by user13783520


Ask a Question

Get help from others!

© 2022 All rights reserved.