2 years ago

Code after middleware

Posted 2 years ago by sorcjc

Hi. I want to execute some code after the middleware (when the validation is OK), in the constructor of my class.

In summary, I have the next:

class QuoteController extends Controller
    private $user;

    public function __construct()

        // Code:
        if (auth()->user()->is_admin)
            $this->user = User::find(session('client_id'));
            $this->user = auth()->user();

I have an attribute named $user to access to the authenticated user (for clients), or in some cases, to the selected user (the administrators can access to the panel of the users, using a session variable).

It works great. But when the session comes to end, I get the next error message:

Trying to get property of non-object

It happens because auth()->user() is null. I can use auth()->check() to avoid it. But I prefer to execute the code after the middleware passes.

How I can achive that? Please give me some ideas.

Thank you in advance!

Please sign in or create an account to participate in this conversation.