laratrust icon indicating copy to clipboard operation
laratrust copied to clipboard

Empty Query Results for Roles when using Teams

Open yusofy opened this issue 3 years ago • 2 comments

  • Laravel Version: 9,0
  • Laratrust Version: 7.0.0

Describe the bug When attempting to get all users in a team and list out the users role for that team, laratrust is not providing any output.

To Reproduce Some of the code snippets I have used are: $users = User::whereRoleIs($roles->pluck('name')->toArray(), $team)->get(); dump($user->allPermissions());

The 2 above, provide the following output: ^ Illuminate\Database\Eloquent\Collection {[#1330 ▼]() #items: [] #escapeWhenCastingToString: false }

Dumping users information does not show any pivot relationships either. It is as if the pivot relationship to query is not working in the models. I've checked the models to make sure the traits are in place.

yusofy avatar Feb 22 '22 03:02 yusofy

  1. How are your models setup?
  2. Did you try disabling cache?
  3. Are you sure there's data in the role user team table?

santigarcor avatar Feb 23 '22 14:02 santigarcor

Hi @santigarcor

Please see the models below. As for cache, I have disabled it and yes...I do have data in my roles, role_user, teams, and users table. It seems that just the querying when working with teams is where I am stuck.

Permission.php

use Laratrust\Models\LaratrustPermission;

class Permission extends LaratrustPermission
{
    public $guarded = [];
}

Role.php

use Laratrust\Models\LaratrustRole;

class Role extends LaratrustRole
{
    public $guarded = [];
}

Team.php

use Laratrust\Models\LaratrustTeam;
use App\Models\User;

class Team extends LaratrustTeam
{
    public $guarded = [];

    // public function users()
    // {
    //     return $this->belongsToMany(User::class, 'role_user')->withTimestamps();
    // }
}

and User.php:

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Casts\Attribute;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Sanctum\HasApiTokens;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laratrust\Traits\LaratrustUserTrait;
use App\Models\Team;

class User extends Authenticatable
{
    use LaratrustUserTrait;
    use HasApiTokens, HasFactory, Notifiable, TwoFactorAuthenticatable;

    /**
     * The attributes that are mass assignable.
     *
     * @var array<int, string>
     */
    protected $fillable = [
        'name',
        'email',
        'password',
    ];

    /**
     * The attributes that should be hidden for serialization.
     *
     * @var array<int, string>
     */
    protected $hidden = [
        'password',
        'remember_token',
    ];

    /**
     * The attributes that should be cast.
     *
     * @var array<string, string>
     */
    protected $casts = [
        'email_verified_at' => 'datetime',
    ];

}

yusofy avatar Feb 24 '22 01:02 yusofy

Tested with a clean install and it works correctly.

This issue has not seen activity in more than a year. I'm going to close it. If you still need help, open another issue.

filippotoso avatar Feb 20 '23 22:02 filippotoso