Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Joining a table to an indexQuery messes up primary table fields on resource list. #625

Closed
pedrodevoto opened this issue Sep 21, 2018 · 3 comments

Comments

@pedrodevoto
Copy link

I have a roles table with a level field that I use to determine the level of permissions a user has. Naturally, every user is able to see every other user of the same level as them and above it. (So, a user with a role of level of 2 should be able to see every user with a role level of 2+)

I overrode the indexQuery method in the User resource to filter out users of a higher role than the role of the authenticated user.

This is my method:

    public static function indexQuery(NovaRequest $request, $query)
    {
        if ($request->user()->isAdmin()) {
            return $query;
        }
        return $query
            ->join('roles', 'roles.id', '=', 'users.role_id')
            ->where('roles.level', '>=', $request->user()->role->level);
    }

The problem is that the roles table also contains a field called name, so in the list of users, the Name column is populated with the role's name, instead of the user's name.

I tried modifying the User's name field to Text::make('Name', 'users.name') but it didn't seem to work.

@bonzai
Copy link

bonzai commented Sep 21, 2018

Try to use select() method:

return $query
	->select('users.*')
    ->join('roles', 'roles.id', '=', 'users.role_id')
    ->where('roles.level', '>=', $request->user()->role->level);

@pedrodevoto
Copy link
Author

@bonzai That worked, thanks. Closing this, although I'm not sure if it is still a bug or not.

@jacob418
Copy link

jacob418 commented Dec 2, 2021

Try to use select() method:

return $query
	->select('users.*')
    ->join('roles', 'roles.id', '=', 'users.role_id')
    ->where('roles.level', '>=', $request->user()->role->level);

Give that man a beer!!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants