laravel 未找到工厂模型
laravel factory model not found
我正在尝试创建一个新项目,其中包含关于 laravel 的更多信息,目前我正在使用工厂创建模型、迁移和种子,并且 运行 正在解决这个问题:
模范用户
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Model implements Authenticatable
{
protected $table = 'user'; //name of the table in database
protected $primaryKey = 'Id'; //Primary Key of the table
/**
* Relations between tables
*/
public function GetLoginInfo()
{
return $this->hasMany('App\Models\LoginInfo', 'UserId');
}
public function getStatus()
{
return $this->belongsTo('App\Models\AccountStatus');
}
}
模型帐户状态
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AccountStatus extends Model
{
protected $table = 'account_status'; //name of the table in database
protected $primaryKey = 'Id'; //primary Key of the table
public $timestamps = false; //true if this table have timestaps
/**
* Relations between tables
*/
public function GetUsers()
{
return $this->hasMany('App\Models\Users', 'StatusId');
}
}
种子文件:
<?php
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Models\User::class, 5)->create();
}
}
出厂档案:
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
//Factory for Account Status table
$factory->define(App\Models\AccountStatus::class, function (Faker\Generator $faker) {
return [
'Description' => $faker->word,
];
});
//Factory for user table
$factory->define(App\Models\User::class, function (Faker\Generator $faker) {
return [
'Username' => $faker->unique()->userName,
'Password' => bcrypt('test'),
'Email' => $faker->unique()->safeEmail,
'Name' => $faker->name,
'StatusId' => Factory(App\Models\AccountStatus::class)->create()->id,
];
});
当尝试使用 artisan 进行 db 种子时:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\Models\Model' not found
已经在尝试 composer dump-autoload 和优化,我将模型放在 App\Models.
的文件夹中
用于帐户状态的工厂种子有效,但是当我尝试为两者(帐户状态然后是用户)运行 时,我遇到了那个错误)有人知道为什么吗?
将所有工厂代码放在 1 个文件中是一种好习惯吗?
在您的 User
模型中,您正在扩展 Model
class 而您应该扩展 Authenticatable
class 别名。
因此您的 User
模型将如下所示:
class User extends Authenticatable
我正在尝试创建一个新项目,其中包含关于 laravel 的更多信息,目前我正在使用工厂创建模型、迁移和种子,并且 运行 正在解决这个问题:
模范用户
<?php
namespace App\Models;
use Illuminate\Foundation\Auth\User as Authenticatable;
class User extends Model implements Authenticatable
{
protected $table = 'user'; //name of the table in database
protected $primaryKey = 'Id'; //Primary Key of the table
/**
* Relations between tables
*/
public function GetLoginInfo()
{
return $this->hasMany('App\Models\LoginInfo', 'UserId');
}
public function getStatus()
{
return $this->belongsTo('App\Models\AccountStatus');
}
}
模型帐户状态
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class AccountStatus extends Model
{
protected $table = 'account_status'; //name of the table in database
protected $primaryKey = 'Id'; //primary Key of the table
public $timestamps = false; //true if this table have timestaps
/**
* Relations between tables
*/
public function GetUsers()
{
return $this->hasMany('App\Models\Users', 'StatusId');
}
}
种子文件:
<?php
use Illuminate\Database\Seeder;
class UserSeeder extends Seeder
{
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
factory(App\Models\User::class, 5)->create();
}
}
出厂档案:
<?php
/** @var \Illuminate\Database\Eloquent\Factory $factory */
//Factory for Account Status table
$factory->define(App\Models\AccountStatus::class, function (Faker\Generator $faker) {
return [
'Description' => $faker->word,
];
});
//Factory for user table
$factory->define(App\Models\User::class, function (Faker\Generator $faker) {
return [
'Username' => $faker->unique()->userName,
'Password' => bcrypt('test'),
'Email' => $faker->unique()->safeEmail,
'Name' => $faker->name,
'StatusId' => Factory(App\Models\AccountStatus::class)->create()->id,
];
});
当尝试使用 artisan 进行 db 种子时:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\Models\Model' not found
已经在尝试 composer dump-autoload 和优化,我将模型放在 App\Models.
的文件夹中用于帐户状态的工厂种子有效,但是当我尝试为两者(帐户状态然后是用户)运行 时,我遇到了那个错误)有人知道为什么吗? 将所有工厂代码放在 1 个文件中是一种好习惯吗?
在您的 User
模型中,您正在扩展 Model
class 而您应该扩展 Authenticatable
class 别名。
因此您的 User
模型将如下所示:
class User extends Authenticatable