2016-11-27 93 views
0

我嘗試創建一個新的項目,都更多地瞭解laravel,現在即時通訊與工廠和進出口運行到這個問題,創建模型,遷移和種子: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 

已經開始嘗試作曲自卸自動加載,優化和我有車型在應用程序\型號的文件夾。

種子與工廠帳戶狀態工作,但當我嘗試運行兩個(帳戶狀態然後用戶)我有這個錯誤)任何人都知道爲什麼? 並且是1個文件中的所有工廠代碼的良好做法?

回答

1

在您的User模型中,您正在擴展Model類,而您應該擴展Authenticatable類別別名。

所以你User模型將作爲看:

class User extends Authenticatable 
+0

工作,所以已可鑑別是一個模式? 另一個問題如何從其他表信息添加外鍵?這不工作:'StatusId'=>工廠(應用\ Models \ AccountStatus :: class) - >創建() - >編號 – syszen

+1

是的,'authenticatable'是一個模型,但也記住它只是'Illuminate \ Foundation \驗證\ User'。對於你的第二個問題,我不確定解決方案,但你可以回答另一個問題。 –