Hi, I'm very begginer with Laravel. I try to use your awesome package.
I found error when running php artisan db:seed
[BadMethodCallException]
Call to undefined method Illuminate\Database\QueryBuilder::assignRole()
Here my UserTableSeeder:
use Illuminate\Database\Seeder;
use Spatie\Permission\Models\Role;
use Spatie\Permission\Models\Permission;
use App\User;
class UserTableSeeder extends Seeder
{
public function run()
{
//--create Role
$adminRole = Role::create(['name' => 'admin']);
$password = 1234;
// ---------- Administrator
$user = User::create([
'name' => 'Administrator',
'email' => '[email protected]',
'password' => $password,
]);
$user->assignRole($adminRole);
or same
$user->assignRole('admin');
Could you explain to me, how to make usertableseeder?
Thanks
I'm guessing that you didn't add the Spatie\Permission\Traits\HasRole to the User.
If you want to learn more about creating seeders, take a look at the official documentation
Here my User model :
use IlluminateAuthAuthenticatable;
use Illuminate\Database\Eloquent\Model;
use IlluminateAuth\Passwords\CanResetPassword;
use IlluminateFoundationAuthAccessAuthorizable;
use Illuminate\ContractsAuthAuthenticatable as AuthenticatableContract;
use Illuminate\ContractsAuthAccessAuthorizable as AuthorizableContract;
use Illuminate\ContractsAuth\CanResetPassword as CanResetPasswordContract;
use Spatie\Permission\Traits\HasRoles;
class User extends Model implements AuthenticatableContract,
AuthorizableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
protected $table = 'users';
protected $fillable = ['name', 'email', 'password'];
protected $hidden = ['password', 'remember_token'];
public function roles()
{
return $this->belongsToMany(Role::class);
}
public function assignRole($role)
{
return $this->roles()->attach($role);
}
}
You imported the namespace but did not add the trait to the model.
Change the line
use Authenticatable, Authorizable, CanResetPassword;
to
use Authenticatable, Authorizable, CanResetPassword, HasRoles;
Ok, it's work.
Many thanks
:+1:
I tried that and it still does not work
Closing the loop on this. As @Omazon said in https://github.com/spatie/laravel-permission/issues/305#issuecomment-362651530 the wrong User model was imported.
I use it in my db seeder , I can add some permissions and role by artisan, but I can't use user model to bind the permissions, It will has a error: Method Illuminate\Database\Eloquent\Collection::assignRole does not exist., I check my model It has Spatie\Permission\Traits\HasRole
I use it in my db seeder , I can add some permissions and role by artisan, but I can't use user model to bind the permissions, It will has a error:
Method Illuminate\Database\Eloquent\Collection::assignRole does not exist., I check my model It hasSpatie\Permission\Traits\HasRole
This is a basic Laravel usage issue.
If you study the error message, it will tell you a lot:
Method Illuminate\Database\Eloquent\Collection::assignRole does not exist
Notice that your object has an Eloquent Collection, not an individual user. This is the same as having an array of objects but you're trying to treat it as though it's only a single object.
Rework your db query to give you a single result ... an actual user object ... before trying to call methods/properties on the object.
Most helpful comment
I'm guessing that you didn't add the
Spatie\Permission\Traits\HasRoleto theUser.If you want to learn more about creating seeders, take a look at the official documentation