[Solved] Class ‘App\Http\Controllers\User’ not found (in UserController.php) Laravel
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.
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 hereclass UserController extends Controller { public function index(){
$user = new User();
dd($user);
}
}
Hope this helped!