K3NN3TH
K3NN3TH

Reputation: 1496

Calling User Model after being authenticated is producing an error Call to undefined method Illuminate\Auth\GenericUser

I have been trying to execute a Method in the User Model after being logged in. It is my understand that you do the following: Auth::user()->foo();

For some reason this is not working.

Code in User.php

<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;

class User extends Eloquent implements UserInterface, RemindableInterface {

    use UserTrait, RemindableTrait;
    protected $table = 'users';
    protected $hidden = array('password', 'remember_token');

    public function foo(){
        return "foo";
    }
}

Code in routes.php

<?php
Route::get('/', function(){
    return View::make('hello');
});

Route::get('users', function(){
    if (Auth::attempt(array('email' => '[email protected]', 'password' => 'Tom')))echo 'Auth-true';
    echo Auth::id();
    if(Auth::check())echo 'checked';
    Auth::user()->foo();
});

echo Auth::id() and the Auth:check() both work, Auth:user()->foo(); fails

Error Message

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Call to undefined method Illuminate\Auth\GenericUser::foo()

Upvotes: 2

Views: 2847

Answers (1)

lagbox
lagbox

Reputation: 50491

Make sure that app/config/auth.php has 'driver' => 'eloquent'. So it will use your Eloquent User model.

Upvotes: 5

Related Questions