Reputation: 305
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?
Upvotes: 0
Views: 76
Reputation: 15786
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.
Upvotes: 1