[英]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,
];
});
嘗試與工匠一起為db種子時:
[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'App\Models\Model' not found
已經嘗試作曲家dump-autoload,優化和我的模型在App \\ Models的文件夾中。
種子與工廠的帳戶狀態工作,但當我嘗試同時運行這兩個帳戶(帳戶狀態,然后用戶)我有那個錯誤)有人知道為什么嗎? 將所有工廠代碼保存在1個文件中是一種好習慣嗎?
在User
模型中,您正在擴展Model
類,而您應擴展Authenticatable
類別名。
因此,您的User
模型將如下所示:
class User extends Authenticatable
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.