mankowitz
1 month ago

limit returned fields from `Auth::user`

Posted 1 month ago by mankowitz

I want to send the name and role of the current user to Vue. I can't figure out how to send what I want.

Auth::user()->load('role') gives this:

=> App\User {#3974
     id: 2,
     role_id: 1,
     name: "xxxx xxxxx",
     email: "[email protected]",
     office_phone: "(414) 858-818",
     avatar: "users/default.png",
     settings: null,
     created_at: "2017-11-21 17:10:28",
     updated_at: "2020-01-15 06:32:14",
     notification_email: null,
     notification_frequency: "i",
     notification_sites: 0,
     deleted_at: null,
     last_login: "2020-01-15 06:32:14",
     token_2fa: "29580",
     token_2fa_expiry: "2030-01-01 00:00:00",
     supervisor_id: null,
     last_login_ip: "127.0.0.1",
     role: TCG\Voyager\Models\Role {#3977
       id: 1,
       name: "admin",
     },
   }

But if I try Auth::user()->load('role:id,name')->only(['name','id','role_id','role.name'])

I get:

=> [
     "name" => "Scott Mankowitz",
     "id" => 2,
     "role_id" => 1,
     "role.name" => null,
   ]

If I try User::with('role:name,id')->find(Auth::user()->id)->select(['name','id','role_id'])->get() then I get a list of every user.

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