0

I need to set a variable in my database every time a connection is opened, get a variable from the token and set the user

SET app_user_id = Auth::id() ?? null;

what is the correct place to set this variable, would be in the AppServiceProvider?

1
  • 1
    For what purpose? Why do you want to do this? Commented Mar 16, 2022 at 14:35

1 Answer 1

1

You can do that with DB::statement. Auth::id() returns null if there is no authenticated user in the default guard, so ?? null is unnecessary.

Putting it in a service provider might work.

public function boot()
{
    DB::statement('SET app_user_id = ?', [Auth::id()]);
}

However, I don't think this will work as you expect it to.

Every user is connecting to the same database and the app_user_id value will be overwritten.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.