我正在尝试创建一个新项目,其中包含了解有关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,
];
});
尝试使用工匠db种子时:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\Models\Model' not found
已尝试使用composer dump-autoload,优化并将模型放在App \ Models中的文件夹中。
种子与工厂的帐户状态工作,但当我尝试运行两个(帐户状态,然后用户)我有这个错误)谁知道为什么? 将所有工厂代码放在1个文件中是个好习惯吗?
答案 0 :(得分:1)
在User
模型中,您正在扩展Model
类,而您应该扩展Authenticatable
类别名。
因此,您的User
模型将显示为:
class User extends Authenticatable