通过身份验证后调用用户模型会产生错误调用未定义的方法 Illuminate\Auth\GenericUser

K3N*_*3TH 2 php authentication laravel-4

我一直在尝试在登录后在用户模型中执行一个方法。据我所知,您执行以下操作: Auth::user()->foo();

由于某种原因,这不起作用。

输入代码 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";
    }
}
Run Code Online (Sandbox Code Playgroud)

输入代码 routes.php

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

Route::get('users', function(){
    if (Auth::attempt(array('email' => 'tom@blah.com', 'password' => 'Tom')))echo 'Auth-true';
    echo Auth::id();
    if(Auth::check())echo 'checked';
    Auth::user()->foo();
});
Run Code Online (Sandbox Code Playgroud)

echo Auth::id() 和 Auth:check() 都有效,Auth:user()->foo(); 失败

错误信息

Symfony \ Component \ Debug \ Exception \ FatalErrorException

Call to undefined method Illuminate\Auth\GenericUser::foo()
Run Code Online (Sandbox Code Playgroud)

lag*_*box 5

确保app/config/auth.php具有'driver' => 'eloquent'. 所以它将使用你的 Eloquent User 模型。