duancheng7743 2016-09-30 15:24
浏览 21
已采纳

Laravel 4:未找到模型类

I am getting a Class 'App\Models\User' not found error when I try too use the User class inside a controller class method. I have looked everywhere and tried everything with no luck! Here's what I've tried:

  1. Check that class exists and is in the right path (it works everywhere else)
  2. Add use App\Models\User; to the top of the controller file and just use User
  3. Tried: new \App\Models\User
  4. Run: composer dump-autoload
  5. Run: php artisan dump-autoload
  6. Run: php artisan clear-compiled

When I do dd(class_exists('App\Models\User')), I get \vendor\laravel\framework\src\Illuminate\Support\helpers.php:513:boolean false which confirms that the class really isn't accessible for some reason.

Any ideas?

EDIT

You will find questions similar to this but not the same. Please read question carefully. I didn't say the controller class was missing. I said a model class (User) was not accessible from inside a particular controller class. And that the model class works everywhere else.

EDIT: Code Excerpt

<?php

use App\Models\User;
use App\Models\Role;
use App\Models\Advert;
use App\Models\AdvertPhoto;
use App\Models\AdvertMetum;
use App\Models\AdvertMetaDatum;
use App\Models\AdvertMetaCategory;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Mail;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Request;

class AdvertsController extends BaseController {

  /**
     * Show the form for creating a new resource.
     * GET /adverts/create
     *
     */
    public function create()
    {
      // New user instance
      // dd(class_exists('\App\Models\User')); // Outputs FALSE
      $userx = new User; // Throws an Exception

      return View::make('adverts.create');

    }




}
  • 写回答

1条回答 默认 最新

  • donglu3184 2016-10-04 12:31
    关注

    I have managed to resolve this on my own. It turns out you have to tell Laravel what class and table will be used for authentication (a.k.a your 'User' class). I didn't know this (plus this is an inherited project). Apparently the User class was defined in the root namespace (i.e. \User) and Laravel was configured to look for \User. But sometimes I see \App\Models\User in the code and it gave me the impression that User was under the same namespace as the other models since they were ALL in the app/models/ folder. I have corrected this problem in config/auth.php by changing:

    'model' => 'User'
    

    to:

    'model' => App\Models\User::class
    

    And adding namespace at the top of app/models/user.php:

    namespace App\Models;
    

    Finally I set an alias in config/app.php like this:

    'User' => 'App\Models\User'
    

    So that where ever I've been using User::blah will not break (forcing me to add use App\Models\User; everywhere!)

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 配置FPT报错,该如何处理
  • ¥15 请大家看一下这个代码咋写,一点思路都没有,最好能做一下,不要伪代码,有偿
  • ¥15 有偿请人帮写个安卓系统下禁止装软件及禁止拷入文件的程序
  • ¥100 用 H.265 对音视频硬编码 (CUDA)
  • ¥20 mpich安装完成后出问题
  • ¥15 stm32循迹小车代码问题
  • ¥15 输入一堆单词,使其去重输出
  • ¥15 qc代码,修改和添加东西
  • ¥50 Unity的粒子系统使用shadergraph(内置管线)制作的一个顶点偏移shader,但是粒子模型移动时,顶点也会偏移
  • ¥15 如何用python处理excel的数据(极值标准化)