Php 未找到Laravel 5用户模型
我已将用户模型从默认应用程序目录移动到app/Models 我已将User中的名称空间更新为Php 未找到Laravel 5用户模型,php,laravel,laravel-5,Php,Laravel,Laravel 5,我已将用户模型从默认应用程序目录移动到app/Models 我已将User中的名称空间更新为namespace-App\Models但我遇到了以下错误: FatalErrorException in EloquentUserProvider.php line 122: Class '\App\User' not found 我的json文件中有正确的条目: "autoload": { "classmap": [ "database", "app/Modu
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/"
}
},
我遗漏了什么?您需要更新您的配置/auth.php文件。将'model'=>'App\User'
更改为'model'=>'App\Models\User'
不要忘记更改您的User.php
命名空间
例如,如果您的用户模型位于/App/User.php
,则第一行应为:
<?php namespace App;
希望这能有所帮助。*注意:我的策略在技术上可能是“不正确的”,因为它污染了全局名称空间。然而,我很难得到其他答案的工作和张贴这只是作为最后的手段*强>
我这样做的方式与其他答案略有不同。我也在使用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'];
}
以下是我的用户表迁移,仅供参考:
<?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');
}
}
希望这有帮助 别忘了换衣服
use App\User;
到
在AuthController.php中,不要忘记在所有以前的更改之后清除缓存
php artisan config:clear
如果您的模型位于特定文件夹中,则需要运行
composer dump-autoload
要刷新:)您是否尝试了composer dump autoload
?@fakemeta是,仍然给出错误:(您是在尝试使用laravel身份验证检索用户对象,还是在手动实例化?@fakemeta我只是在尝试获取开箱即用的起始页。当我尝试登录时,它会给我该错误。另外请注意:自动加载
块的psr-4
部分的第二行这可能会污染全局名称空间,但这是使其工作的唯一方法如果您遵循其他答案,包括我的答案,则不会。我理解您的反对票。在发布我自己的答案之前,我尝试过其他答案,但无法使其发挥作用。我将修改我的答案,建议人们在我之前尝试其他技巧。对我有效。您是如何发现这一点的?
<?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;
php artisan config:clear
composer dump-autoload