entrust
entrust copied to clipboard
[BadMethodCallException] Call to undefined method Illuminate\Database\Query\Builder::attachRole()
I am getting the following error when I try to run php artisan db:seed --class=UserTableSeeder
:
[BadMethodCallException]
Call to undefined method Illuminate\Database\Query\Builder::attachRole()
I have created a UserTableSeeder.php file that looks like this:
<?php
use Illuminate\Database\Seeder;
use App\User;
use App\Role;
class UserTableSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
$users = [
[
'name' => 'test_admin',
'email' => '[email protected]',
'password' => Hash::make('123456789')
],
[
'name' => 'test_view',
'email' => '[email protected]',
'password' => Hash::make('123456789')
],
[
'name' => 'test_manage',
'email' => '[email protected]',
'password' => Hash::make('123456789')
]
];
foreach ($users as $key => $value) {
User::create($value);
}
/*test_admin*/
$user=User::all()->first();
$user->attachRole(1);
/*test_view*/
$user = User::find(2);
$user->attachRole(2);
/*test_manage*/
$user = User::find(3);
$user->attachRole(3);
}
}
Any suggestion?
My solution is:
DB::table('users')->insert([
'name' => 'Admin User',
'email' => '[email protected]',
'password' => bcrypt('secret'),
]);
DB::table('users')->insert([
'name' => 'Rental Admin',
'email' => '[email protected]',
'password' => bcrypt('secret'),
]);
DB::table('users')->insert([
'name' => 'Normal User',
'email' => '[email protected]',
'password' => bcrypt('secret'),
]);
DB::table('users')->insert([
'name' => 'Agent User',
'email' => '[email protected]',
'password' => bcrypt('secret'),
]);
$user1 = DB::table('users')->where('email', '[email protected]')->value('id');
$user2 = DB::table('users')->where('email', '[email protected]')->value('id');
$user3 = DB::table('users')->where('email', '[email protected]')->value('id');
$user4 = DB::table('users')->where('email', '[email protected]')->value('id');
DB::table('role_user')->insert([
'user_id' => $user1,
'role_id' => 1,
]);
DB::table('role_user')->insert([
'user_id' => $user2,
'role_id' => 2,
]);
DB::table('role_user')->insert([
'user_id' => $user3,
'role_id' => 3,
]);
DB::table('role_user')->insert([
'user_id' => $user4,
'role_id' => 4,
]);
I had same issue. I was using Ultraware Roles https://github.com/ultraware/roles ... my fix was using a Trait from the package
//in User model namespace use:
use Illuminate\Notifications\Notifiable;
use Illuminate\Foundation\Auth\User as Authenticatable;
// below one extra
use Ultraware\Roles\Traits\HasRoleAndPermission;
//then in User Class:
use Notifiable, HasRoleAndPermission;
This solved problem. Hope this helps for your case!
@hasnatbabur worked perfect, thanks for that !
thank you