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

User1980's avatar

Problem getting Spattie role outside of the user model(from another model)

Hi all,

I am using eloquent and spattie v3 for the roles and permissions.

I have a CHAT model with a user_id.

I would like to get the role of each user of a chat:

Chat::with(['user' => function ($query) {
                $query->select('id', 'email', 'first_name', 'last_name');
            }])
->with('roles')
->orderBy('id', 'DESC')
 ->paginate(5);

Unfortunately, I am getting the error "Unable to get the requested data!"

Spattie uses these tables:

ROLES:
-id
-name
-guard_name
-created_at
-updated_at
MODEL_HAS_ROLES
-role_id
-model_type
-model_id

What shall I add in the CHAT model to reach a role please?

return $this->hasManyThrough on user model but to which model? As Spattie has not created a model in the model folder of my app.

I am confused.

Thank you.

0 likes
5 replies
Sinnbeck's avatar
Sinnbeck
Best Answer
Level 102

Why does your chat have a role?

This makes more sense

Chat::with(['user' => function ($query) {
                $query->select('id', 'email', 'first_name', 'last_name')->with('roles');
            }])
->orderBy('id', 'DESC')
 ->paginate(5);
1 like
User1980's avatar

@Sinnbeck lol, sorry sorry!! I did not explain myself properly.....yes I meant the user part of the chat...oups :-)

I see, I did not know we could pass the roles this way, I thought it was always based on the main top model of the initial query.

Got it!

Thank you so much as always @sinnbeck

Please or to participate in this conversation.