Laravel 5 Model namespace class not found
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.
Comments
-
imperium2335 over 1 year
I am trying to store my models in a custom namespace and directory structure as shown 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']); });