在 Laravel 5 中找不到“App\users”类
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/37031327/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Class 'App\users' not found in Laravel 5
提问by Sebastian
I am getting the following error:
我收到以下错误:
FatalErrorException in UserController.php line 23:
Class 'App\Users' not found
UserController.php 第 23 行中的 FatalErrorException:
未找到“App\Users”类
Usercontroller.php:
用户控制器.php:
<?php
namespace App\Http\Controllers;
use Request;
use App\Users;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Input;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Validator;
use Redirect;
use Session;
class UserController extends Controller
{
public function index()
{
$user=Users::all();
return view('users.index',compact('user'));
}
public function edit($id)
{
$user=Users::find($id);
return view('users.edit',compact('user'));
}
public function update($id)
{
$userUpdate=Request::all();
$user=Users::find($id);
$user->update($userUpdate);
return redirect('users');
}
public function destroy($id)
{
Users::find($id)->Delete();
return redirect('users');
//Schema::table('books', function ($table) {
//$table->softDeletes();
//});
}
}
Users.php (its in App/Users.php):
Users.php(在 App/Users.php 中):
namespace App;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
class User extends Model
{
protected $fillable=[
'name',
'email',
'password',
'remember_token',
'created_at',
'updated_at',
'active',
'role'
];
}
I tried the composer dumpautoload
and php artisan config:clear
as well, but it doesn't seem to work.
我也尝试了composer dumpautoload
和php artisan config:clear
,但它似乎不起作用。
回答by Josh
\App\User
, not \App\Users
. That's all.
\App\User
,不是\App\Users
。就这样。
Your class name and file name do not match. Either change the class to Users
or file to User
.
您的类名和文件名不匹配。将类更改为Users
或文件更改为User
.
回答by Alexey Mezenin
You forgot to use Model
. Add use App\User;
after namespace
clause.
你忘了use Model
。添加use App\User;
afternamespace
子句。
Also, rename Users.php
to User.php
.
此外,重命名Users.php
为User.php
.
Or you can use this model it like this:
或者你可以像这样使用这个模型:
App\User::find(1);