Laravel 5 Model namespace class not found

10,861

Solution 1

If your controller is stored in Modules/Core/Controllers, the namespace should be namespace Modules\Core\Controllers;

And likewise, if the model is stored in Modules/Core/Models, its namespace is namespace Modules\Core\Models;

Then, in the controller, import it before using it:

<?php namespace Modules\Core\Controllers;

use Modules\Core\Models\User;
use App\Http\Controllers\Controller;

class TestController extends Controller {

    public function index(){
        $user = new User;
        return view('core::test');
    }

}

Solution 2

I had the same problem as above. In my case I had the following namespace:

namespace Modules\XMLApi;

I got the same error as above. When I changed the namespace to the following:

namespace Modules\XmlApi;

Then run the following command: composer dump-autoload

Then it worked.

Share:
10,861
imperium2335
Author by

imperium2335

PHP, JS, MySQL coder and retired 3D modeler.

Updated on July 25, 2022

Comments

  • imperium2335
    imperium2335 over 1 year

    I am trying to store my models in a custom namespace and directory structure as shown here:

    enter image description here

    I have:

    namespace Modules\Core;
    
    use App\Http\Controllers\Controller;
    
    class TestController extends Controller {
    
        public function index(){
            $user = new User;
            return view('core::test');
        }
    
    }
    

    But I am getting:

    FatalErrorException in TestController.php line 8:
    Class 'Modules\Core\User' not found
    

    Which is wrong anyway, so I thought it must be 'Modules\Core\Models\User'. I tried this and I still got the same error (just with a different class name).

    My model:

    namespace Modules\Core;
    
    use Illuminate\Database\Eloquent\Model as Eloquent;
    
    class User Extends Eloquent {
        protected $table = 'users';
    }
    

    How can I get access to this model in the TestController?


    Route::group(array('namespace' => 'Modules\Core\Controllers'), function() {
        Route::get('/test', ['uses' => 'TestController@index']);
    });