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

martinszeltins's avatar

How to add custom fields to auth session?

I have a users table and a profiles table in my database. I would like to have also everything from my profiles table for that user be stored in auth session. Right now my web auth looks like this:

array:2 [
  "name" => 23
  "user" => array:20 [
    "id" => 23
    "user" => "Martin"
    "password" => "6edasfasf1449asdf4asdf984989a"
    "role" => "administrator"

I would like it to also fetch and store everything from user's profile so auth session would look like this..

array:3 [
  "name" => 23
  "user" => array:20 [
    "id" => 23
    "user" => "Martin"
    "password" => "6edasfasf1449asdf4asdf984989a"
    "role" => "administrator"
  "profile" => array:20 [
    "id" => 12
    "fullname" => 'Martin Zeltin'
    "phone_number" => '+125555978656'
    "address" => '1600 Amphitheatre Parkway, Mountain View, CA'

How could I add this to my auth session? I know that a query could be as simple as select * from profiles where user_id = ? but how to add this to auth session?

0 likes
2 replies
Cronix's avatar

In user model, just add

protected $with = ['profile'];

assuming you have a profile() relationship set up on your model. It's the same as doing User::with('profile'), except it will do it automatically whenever a User is retrieved, so you can access it with Auth::user()->profile, or $request->user()->profile, or whenever you manually retrieve a user.

martinszeltins's avatar

What if I don't want it every time a user is retrieved but only for auth session? Is that possible?

Please or to participate in this conversation.