1

I've another relation that attach to role but when I tried to call it in controller with with/load/relations model it doesn't work. I can't call any relationship in role model except permissions.

class Role extends \Spatie\Permission\Models\Role
{
    public static $rules = [
        'name'=>'required'
    ];
    //protected $relations = ['satkers'];
    //protected $with = ['satkers'];

    //RELATIONS
    public function satkers(){
        return $this->belongsTo(MasterSatker::class, 'master_pelaksana_satker_id', 'id')->withDefault(['name'=>'']);
    }
}

How call it in controller? I've tried show it like this $role->load('satkers') and $role->satkers, it doesn't work. I got an error Call to undefined relationship [satkers] on model [Spatie\Permission\Models\Role] and null. I tried dd the $role, here is it what i got

Spatie\Permission\Models\Role {#1587 ▼
  #guarded: array:1 [▶]
  #connection: "mysql"
  #table: "roles"
  #primaryKey: "id"
  #keyType: "int"
  +incrementing: true
  #with: []
  #withCount: []
  #perPage: 15
  +exists: true
  +wasRecentlyCreated: false
  #attributes: array:6 [▶]
  #original: array:6 [▶]
  #changes: []
  #casts: []
  #classCastCache: []
  #dates: []
  #dateFormat: null
  #appends: []
  #dispatchesEvents: []
  #observables: []
  #relations: array:1 [▼ //it just show permissions
    "permissions" => Illuminate\Database\Eloquent\Collection {#1975 ▶}
  ]
  #touches: []
  +timestamps: true
  #hidden: []
  #visible: []
  #fillable: []
  -permissionClass: null
}
apokryfos
  • 38,771
  • 9
  • 70
  • 114
Ray Coder
  • 1,022
  • 3
  • 15
  • 30
  • I would call the Class differently because "Role" Class already exists. – David Mar 26 '21 at 14:58
  • And you get an instance of the Spatie Class it seems, not yours. Check what you are calling – David Mar 26 '21 at 14:59
  • Got it. Thanks a lot, i've change it in config and change the imported role in controller and it works! – Ray Coder Mar 26 '21 at 15:12
  • @RayCoder ... I am facing the same issue ... can you share your code ? – FaISalBLiNK Jul 07 '22 at 09:06
  • @FaISalBLiNK Make sure you dont call (import) Role from spatie but from the model you created (`use App\Model\Role`), if you call from spatie and from your model make sure it has different name – Ray Coder Jul 07 '22 at 09:43
  • @RayCoder thanks ... I am trying to return the relation for the User model ... in the User model I only imported `use Spatie\Permission\Traits\HasRoles;` – FaISalBLiNK Jul 07 '22 at 09:55
  • @FaISalBLiNK Can you explain the details of your problem? Maybe i can help – Ray Coder Jul 07 '22 at 10:00
  • @RayCoder ... I created a many-to-many relation between `ItPolicy` and `Role` models but when I try to retrieve `User::where('email', $request->email) ->with(['profile', 'positionSummary', 'contractSummary', 'roles.itPolicies', 'practices']) ->firstOrFail();` I get this error message `Call to undefined relationship [itPolicies] on model [Spatie\\Permission\\Models\\Role].` – FaISalBLiNK Jul 07 '22 at 10:37
  • Have you read this? https://github.com/spatie/laravel-permission/discussions/2137 @FaISalBLiNK – Ray Coder Jul 15 '22 at 03:44
  • @RayCoder thanks ... I have gone through the documentation for the Spatie laravel-permissions package and figured out how to extend the Spatie's Role class – FaISalBLiNK Jul 20 '22 at 08:43

0 Answers0