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

abdulrafey38's avatar

__construct in Laravel Controllers

    public function __construct()
    {
        $this->middleware('auth:instructor')->except('attendanceShowToStudent');
    }

Can anyone here please explain me whats the purpose of __construct() in laravel and what in the above code __construct() do, and what about except() whats the purpose of it.

0 likes
7 replies
tykus's avatar
tykus
Best Answer
Level 104

The constructor is a method that is called whenever a new instance of a (PHP) class is instantiated. This is object oriented programming 101.

The code you shared will apply the auth:instructor middleware on all controller actions (in that class) except the action called attendanceShowToStudent. The auth:instructor middleware (most likely) requires the authenticated user accessing those actions to be an Instructor type user in your application; so, if you are authenticated as a student, you will not be able to access those routes pointing to controller actions protected by the auth:instructor middleware.

1 like
abdulrafey38's avatar

Thanks Appreciated I got it know plus can you tell me that can i put more than one actions or method in except ().

jlrdw's avatar

can we add more than one method in except()

If more just separate with a comma.

1 like
jlrdw's avatar

Sorry if you did not trust the link I gave, but https://www.w3schools.com is a safe site. I suggest you learn some more PHP before going too far in laravel, it's a PHP framework.

1 like

Please or to participate in this conversation.