https://www.w3schools.com/php/php_oop_constructor.asp
->except says exactly that, apply auth:instructor to all methods except attendanceShowToStudent
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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.
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.
Please or to participate in this conversation.