0
votes

I have a Role Model

class Role extends EntrustRole
{
  public function permissions()
  {
    return $this->belongsToMany('App\Permission', Config::get('entrust::permission_role_table'));
  }
}

Now i would like to delete a certain role

$role = Role::where("display_name",'=', $request->route("role"))->first();
$role->delete() //fails
$role->forceDelete() //also fails

The above returns an error

message www/html/laravel/keybrands/vendor/laravel/framework/ src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php"

"Class name must be a valid object or a string"

The permission model is in the App namespace.

Where could i be going wrong?

3
Show the role modal - Vikash
@Vikash role model is in the question - Geoff
"Where could i be going wrong". Error: "Class name must be a valid object or a string". Keyword: Class name. Where do you use class names in the code you gave us? In belongsMany method. So, there's your debugging 101 (there's also the whole error stack trace that would give you that particular line as well, make use of it). - Mjh
probably there is a Role class already, change the class name to something temporary and try it out. - Ali
I see the problem was actually caused by the Entrust trait.By adding the users relationship in the role model now works. - Geoff

3 Answers

2
votes

Try it like this:

class Role extends EntrustRole
{

    public function permissions()
    {
        return $this->belongsToMany(Permission::class, config('entrust::permission_role_table'));
    }
}
0
votes

Checking on the stacktrace showed the error being caused by the entrust trait.

I have managed top resolve this by adding to the role model.

    public function users()
{
    return $this->belongsToMany(Config::get('auth.providers.users.model'),Config::get('entrust.role_user_table'),Config::get('entrust.role_foreign_key'),Config::get('entrust.user_foreign_key'));

}
0
votes

Define User model in config/auth.php:

<?php

return [
    /*
    |--------------------------------------------------------------------------
    | Defining Model
    |--------------------------------------------------------------------------
    */
    'model' => App\User::class,
];