gpt4 book ai didi

php - Laravel 5 播种

转载 作者:可可西里 更新时间:2023-11-01 00:13:44 30 4
gpt4 key购买 nike

我正在关注 docs为 users 表设置种子,该表显示正在使用 User::create

class UserTableSeeder extends Seeder {

public function run()
{
DB::table('users')->delete();

User::create([
'username' => 'test',
'firstname' => 'Test',
'lastname' => 'Test',
'email' => 'test@domain.com',
'password' => Hash::make('test'),
'role' => 'user'
]);
}

}

但它一直在说:

PHP Fatal error:  Class 'User' not found in /home/vagrant/projects/roopla/database/seeds/UserTableSeeder.php on line 17

我想也许我需要 make:model using artisan for User,但它已经存在了。任何人都可以指出我正确的方向我只是刚刚开始使用 Laravel 并通过 Laracast 和文档将它拼凑在一起,但是没有 Laracast 用于 5.0 的播种。您现在似乎无法生成种子,因为 artisan 无法识别 generate:seed

最佳答案

那是因为在 Laravel 5 中,User 模型位于 App 命名空间中,而您的播种器位于全局命名空间中。

你应该使用:

 \App\User::create([
'username' => 'test',
'firstname' => 'Test',
'lastname' => 'Test',
'email' => 'test@domain.com',
'password' => Hash::make('test'),
'role' => 'user'
]);

或在种子文件的开头:

use App\User;

现在你可以使用你展示的代码了

关于php - Laravel 5 播种,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28488584/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com