在 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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 13:43:32  来源:igfitidea点击:

Class 'App\users' not found in Laravel 5

phplaravel

提问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 dumpautoloadand php artisan config:clearas well, but it doesn't seem to work.

我也尝试了composer dumpautoloadphp 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 Usersor file to User.

您的类名和文件名不匹配。将类更改为Users或文件更改为User.

回答by Alexey Mezenin

You forgot to use Model. Add use App\User;after namespaceclause.

你忘了use Model。添加use App\User;afternamespace子句。

Also, rename Users.phpto User.php.

此外,重命名Users.phpUser.php.

Or you can use this model it like this:

或者你可以像这样使用这个模型:

App\User::find(1);