我正在使用Laravel。我正在创建我的数据库和种子文件。 (不使用Test Dummy)当我启动终端进程时,我得到了一个Missing Argument错误。
khazax@khz ~/Code/Laravel $ php artisan db:seed
Seeded: UsersTableSeeder
Seeded: RolesTableSeeder
[ErrorException]
Missing argument 1 for Illuminate\Database\Eloquent\Builder::where()
khazax@khz ~/Code/Laravel $
DatabaseSeeder.php
<?php
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Model::unguard();
$this->call(UsersTableSeeder::class);
$this->call(RolesTableSeeder::class);
$this->call(PermissionsTableSeeder::class);
}
}
UsersTableSeeder.php
<?php
use App\Models\User;
use Illuminate\Database\Seeder;
class UsersTableSeeder extends Seeder
{
public function run()
{
User::truncate();
User::create([
'first_name' => 'Onur',
'last_name' => 'Kaya',
'username' => 'Khazax',
'username_slug' => 'khazax',
'email' => 'onurkayaes@gmail.com',
'password' => bcrypt('123'),
'confirmation_code' => md5(microtime()+env('APP_KEY')),
'confirmed' => 1,
'settings' => [
'gender' => 'Erkek',
'phone' => '111',
'adress' => 'Eskisehir/Turkiye',
'home_page' => 'https://khazadum.com',
'opt_in_monhtly' => false,
'opt_in_quartly' => false,
'opt_in_year' => true
]
]);
User::create([
'first_name' => 'Tab',
'last_name' => 'Dev',
'username' => 'Tabdev',
'username_slug' => 'tabdev',
'email' => 'info@123.com',
'password' => bcrypt('123'),
'confirmation_code' => md5(microtime()+env('APP_KEY')),
'confirmed' => 1,
'settings' => [
'gender' => 'Erkek',
'phone' => '222',
'adress' => 'Istanbul/Turkey',
'home_page' => 'https://tabtusu.com',
'opt_in_monhtly' => false,
'opt_in_quartly' => false,
'opt_in_year' => true
]
]);
}
}
RolesTableSeeders.php
<?php
use App\Models\Role;
use App\Models\User;
use App\Models\AssignedRole;
use Illuminate\Database\Seeder;
class RolesTableSeeder extends Seeder
{
public function run()
{
Role::truncate();
$adminRole = new Role;
$adminRole->name = 'admin';
$adminRole->display_name = 'Administrator';
$adminRole->description = 'Tüm yetkilerin var olduğu kullanıcı grubu';
$adminRole->is_admin = 1;
$adminRole->save();
$userRole = new Role;
$userRole->name = 'user';
$userRole->display_name = 'User';
$userRole->description = 'Sadece site içi işlemleri yapabilen kullanıcı grubu';
$userRole->is_admin = 0;
$userRole->save();
$admin = User::where('email', 'onurkayaes@gmail.com')->first();
$assRoleAdmin = new AssignedRole;
$assRoleAdmin->user_id = $admin->id;
$assRoleAdmin->role_id = $adminRole->id;
$assRoleAdmin->save();
$user = User::where('email', 'info@123.com')->first();
$assRoleUser = new AssignedRole;
$assRoleUser->user_id = $user->id;
$assRoleUser->role_id = $userRole->id;
$assRoleUser->save();
}
}
和PermissionsTableSeeders.php
<?php
use App\Models\Permission;
use App\Models\Role;
use Illuminate\Database\Seeder;
class PermissionsTableSeeder extends Seeder
{
public function run()
{
Permission::truncate();
$permission = [
array(
'name' => 'manage_user',
'display_name' => 'Manage Users',
'is_admin' => 1
),
array(
'name' => 'manage_content',
'display_name' => 'Manage Content',
'is_admin' => 1
),
array(
'name' => 'manage_roles',
'display_name' => 'Manage User Roles',
'is_admin' => 1
),
array(
'name' => 'manage_files',
'display_name' => 'Manage Files',
'is_admin' => 1
)
];
foreach($permission as $row)
{
$row = array_merge($row, ['created_at' => new DateTime, 'updated_at' => new DateTime]);
DB::table('permissions')->insert($row);
}
$role_id_admin = Role::where()->first()->id;
$perm_base = (int)DB::table('permissions')->first()->id-1;
$perms = [
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 1
),
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 2
),
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 3
),
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 4
),
array(
'role_id' => $role_id_admin,
'permission_id' => $perm_base + 5
)
];
DB::table('permission_role')->delete();
foreach($perms as $row)
{
$row = array_merge($row, ['created_at' => new DateTime, 'updated_at' => new DateTime]);
DB::table('permission_role')->insert($row);
}
}
}
我错过了一些代码还是仅仅是一个简单的拼写错误?在此先感谢您的帮助。
答案 0 :(得分:0)
代码中的问题是该行:
$role_id_admin = Role::where()->first()->id;
PermissionsTableSeeder
中的。
根据您的逻辑,它应如下所示:
$role_id_admin = Role::first()->id;
或者您还应该为此添加条件:
$role_id_admin = Role::where('name','admin')->first()->id;