2

Iam trying to delete a role

$role = Role::findOrFail(1);
$role->delete();

I got the following error

FatalErrorException in Model.php line 945:
Class name must be a valid object or a string

in vendor/zizaco/entrust/src/commands/MigrationCommand.php on line 86

$usersTable  = Config::get('auth.providers.users.table');
$userModel   = Config::get('auth.providers.users.model');

Role model class

namespace App\Models;

use Zizaco\Entrust\EntrustRole;

class Role extends EntrustRole
{
       protected $fillable =  ['name', 'display_name', 'isActive','description', 'created_at', 'updated_at'];

}

3 Answers3

4

I think this is the problem:

Find the file: vendor/zizaco/entrust/src/Entrust/Traits/EntrustRoleTrait.php

Replace

Line 51: ... Config::get('auth.model') ...

with

Line 51: ... Config::get('auth.providers.users.model') ...

dimitrisd
  • 566
  • 1
  • 9
  • 30
0

Its not good practice to update anything in core packages because in case you update your package, this change will be replace, instead override that function by adding this in your App\Role.php

/**
 * Many-to-Many relations with the user model.
 *
 * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
 */
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'));
   // return $this->belongsToMany(Config::get('auth.model'), Config::get('entrust.role_user_table'));
}
Tarunn
  • 1,038
  • 3
  • 23
  • 45
0

I agree with @Tarunn. If you want code simplicity....

use App\User;

/**
* Many-to-Many relations with the user model.
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/

public function users()
{
   return $this->belongsToMany(User::class);
}
Sand Of Vega
  • 2,297
  • 16
  • 29