[英]Laravel 5 User Model not found
我已將用戶模型從默認應用程序目錄移至應用程序/模型。
我已經將User中的namespace App\\Models;
更新為namespace App\\Models;
但我收到此錯誤:
FatalErrorException in EloquentUserProvider.php line 122:
Class '\App\User' not found
我的json文件中有正確的條目:
"autoload": {
"classmap": [
"database",
"app/Modules",
"app/Models"
],
"psr-4": {
"App\\": "app/",
"Modules\\": "app/Modules/"
}
},
我錯過了什么?
您需要更新config / auth.php文件。 將'model' => 'App\\User'
更改為'model' => 'App\\Models\\User'
。
之前所有更改后,請不要忘記清除緩存
php artisan config:clear
不要忘記更改User.php
命名空間。
例如,如果您的用戶模型位於/App/User.php
則第一行應顯示為:
<?php namespace App;
但是,如果您已經創建了/Models
目錄,並且您的User模型現在位於/App/Models/User.php
,則User.php的第一行應引用此新名稱空間:
<?php namespace App\Models;
希望能有所幫助。
*注意:我的策略在技術上可能是“錯誤的”,因為它會污染全局名稱空間。 但是,我很難使其他答案起作用,並且僅在萬不得已時才發布此答案。 *
我這樣做的方式與其他答案略有不同。 我也在使用Laravel 5。
1)我創建了app / Models目錄,並將User.php移到該目錄中。
2)我通過刪除頂部的名稱空間來修改/app/Models/User.php。 這可能會污染全局名稱空間,但這是使它起作用的唯一方法。
<?php // !!!! NOTICE no namespace here !!!!!
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements AuthenticatableContract, CanResetPasswordContract {
use Authenticatable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['first_name', 'last_name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
}
3)我在composer.json的自動加載部分中添加了“ app / Models”:
"autoload": {
"classmap": [
"database",
"app/Models" // <==== I added this
],
"psr-4": {
"App\\": "app/"
}
},
4)我修改auth.php如下:
/*
|--------------------------------------------------------------------------
| Authentication Model
|--------------------------------------------------------------------------
|
| When using the "Eloquent" authentication driver, we need to know which
| Eloquent model should be used to retrieve your users. Of course, it
| is often just the "User" model but you may use whatever you like.
|
*/
'model' => '\User',
作為參考,這是我的users表遷移:
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('first_name');
$table->string('last_name');
$table->string('email')->unique();
$table->string('password', 60);
$table->rememberToken();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
這是我的UsersTableSeeder.php文件:
<?PHP
use Illuminate\Database\Seeder;
use Illuminate\Database\Eloquent\Model;
class UsersTableSeeder extends Seeder {
public function run()
{
DB::table('users')->delete();
$users = [
[ 'id' => 1, 'first_name' => 'Bret', 'last_name' => 'Lastname', 'email' => 'email@domain.com', 'password' => Hash::make('my raw dev password')]
];
foreach($users as $user){
\User::create($user);
}
}
}
?>
完成所有這些更新之后,在命令行上運行:
composer dump-autoload
php artisan migrate --seed
希望這可以幫助!
別忘了改變
use App\User;
至
use App\Models\User;
在AuthController.php中
如果您的模型位於特定的文件夾中,則需要運行
composer dump-autoload
刷新:)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.