2

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?

Mere Development
  • 2,439
  • 5
  • 32
  • 63
emen
  • 6,050
  • 11
  • 57
  • 94

2 Answers2

4

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 ;)
}
Hexodus
  • 12,361
  • 6
  • 53
  • 72
2

This is actually a comment to @Hexodus response, but I don't have the required points to comment.

You can actually have your User authentication named whatever you want, for instance

  • Model: Turtle
  • Table: turtles

But you have to go into app\config\auth.php and change the 'model' => '...' and 'table' => '...' values in order for Laravel's authentication to work.

Also, according to the docs, you don't even need 'username' or 'password' explicitly defined as such in your database

if (Auth::attempt(array('email' => $email, 'password' => $password)))
{
  return Redirect::intended('dashboard');
}

Take note that 'email' is not a required option, it is merely used for example. You should use whatever column name corresponds to a "username" in your database. The Redirect::intended function will redirect the user to the URL they were trying to access before being caught by the authentication filter. A fallback URI may be given to this method in case the intended destination is not available.

Effectively, 'email' in this instance is considered a 'username'


Edit, and because I was having trouble with this at first, you do not need to hash passwords when you use Auth::attempt(...).

adamellsworth
  • 361
  • 1
  • 3
  • 15