Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel 5 - FatalErrorException: Class 'User' not found

I am very new to Laravel and just started setting up things with Laravel 5. I am attempting to create a simple user authentication app with Laravel.

I created register.blade.php which includes form to register user.

Here is my routes.php

Route::post('/register', function()
{
$user = new User;  
$user->email = Input::get('email');
$user->username = Input::get('username');
$user->password = Hash::make(Input::get('password'));
$user->save();
$theEmail = Input::get('email');
return View::make('thanks')->with('theEmail', $theEmail);
});

Here is the section of register.blade.php which creates form for the user registration.

{!! Form::open(array('url' => 'register')) !!}
       {!! Form::label('email', 'Email Address') !!}
       {!! Form::text('email') !!}

       {!! Form::label('username', 'Username') !!}
       {!! Form::text('username') !!}

       {!! Form::label('password', 'Password') !!}
       {!! Form::password('password') !!}

       {!! Form::submit('Sign Up') !!}
{!! Form::close() !!}

I am getting this error when I click on Sign Up button on register page.

in routes.php line 30 at HandleExceptions->fatalExceptionFromError(array('type' => '1', 'message' => 'Class 'User' not found', 'file' => 'C:\xampp\htdocs\urlshort\app\Http\routes.php', 'line' => '30')) in HandleExceptions.php line 116 at HandleExceptions->handleShutdown()

After going through few queries in Google, I realized I forgot to load the User class. So, I included link to file with User class in composer.json file

    "autoload": {
    "classmap": [
        "database",
        "app/User.php"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

I ran the composer dump-autoload command. But I am still getting the same error. I am unable to figure out where my code went work.

like image 272
Rajeeb Avatar asked Feb 19 '15 15:02

Rajeeb


2 Answers

try to use

$user = new \App\User;

instead

$user = new User;

like image 99
xcerx Avatar answered Oct 03 '22 06:10

xcerx


I had the same issue, the answer above didn't help, but I used

use App\Models\Access\User\User; instead of use User; in my controller and it worked.

like image 37
Patrick Mutwiri Avatar answered Oct 03 '22 07:10

Patrick Mutwiri