[Solved] Class ‘App\Http\Controllers\User’ not found (in UserController.php) Laravel

Sumedha Gn
2 min readNov 24, 2020

--

I’ve recently embarked on a journey to learn Laravel and while going through a tutorial, I encountered this error when trying to create a new user on UserController.php. I’m currently using Laravel ver 8.15.0. Many suggested adding ‘use App\User;’ however, it still gave me the same error. Here’s the code I was trying to run:

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;class UserController extends Controller {   public function index(){
$user = new User(); //This was giving the error
dd($user);
}
}

To resolve this error, it was more straightforward than expected.

For Laravel 8.x, add into UserController.php:

use App\Models\User;

For older versions:

use App\User;

Why ‘use App\User;’ did not work was because, in User.php, the namespace is App\Models instead of App.

User.php namespace (Laravel 8.15)

Your UserController.php should look something like this and the error should be resolved:

<?phpnamespace App\Http\Controllers;use Illuminate\Http\Request;
use App\Models\User; // Add here
class UserController extends Controller { public function index(){
$user = new User();
dd($user);
}
}

Hope this helped!

--

--

Sumedha Gn

Documenting errors I’ve faced so you can save time trying to resolve them, feel free to let me know how I can improve too!