Search code examples
phplaraveleloquentlaravel-3

Class 'User' not found in Laravel


I keep getting

Unhandled Exception
Message:

Class 'User' not found

Location:

C:\wamp\www\laravel\laravel\auth\drivers\eloquent.php on line 70

When I'm logging in a user. I don't think there's any problem in eloquent.php. Please take a look at my login controller:

class Login_Controller extends Base_Controller {

    public $restful = true;

    public function get_index(){
        return View::make('login');
    }

    public function post_index(){

        $username = Input::get('username');
        $password = Input::get('password'); 
        $user_details = array('username' => $username, 'password' => $password);

        if ( Auth::attempt($user_details) )
        {
            return Redirect::to('home.index');
        }
        else
        {
            return Redirect::to('login')
                ->with('login_errors', true);
        }


    }
}

This is the view of login:

{{ Form::open('login') }}
    <!-- username field -->
    <p>{{ Form::label('username', 'Username') }}</p>
    <p>{{ Form::text('username') }}</p>
    <!-- password field -->
    <p>{{ Form::label('password', 'Password') }}</p>
    <p>{{ Form::password('password') }}</p>
    <!-- submit button -->
    <p>{{ Form::submit('Login', array('class' => 'btn btn-primary')) }}</p>
{{ Form::close() }}

And routes.php:

<?php

Route::controller(Controller::detect()); // This line will map all our requests to all the controllers. If the controller or actions don’t exist, the system will return a 404 response.
Route::get('about', 'home@about');


Route::filter('auth', function()
{
    if (Auth::guest()) return Redirect::to('login');
});

I used Eloquent as my authentication driver. I've tried changing it to Fluent, but after I click login button, it displays a login error made by this line return Redirect::to('login')->with('login_errors', true); in the else statement.

What's wrong with 'User' class when using Eloquent?


Solution

  • Mike's right, this is because you've got no User model but there's more ...

    The line where laravel's searching for the user model and not finding it is the following:

    if ( Auth::attempt($user_details) )
    

    This happens because Laravels authentification system uses per default the eloquent driver. To satisfy Laravel you need a database table named 'users' with at least the columns 'username' and 'password' of type text and maybe also the columns 'created_at' and 'updated_at' when using timestamps but you can switch it off.

    \application\models\user.php
    
    <?PHP 
    class User extends Eloquent
    {    
        public static $timestamps  = false; //I don't like 'em ;)
    }