我一直在尝试在登录后在用户模型中执行方法。我了解到您执行以下操作: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";
}
}
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();
});
echo Auth :: id()和Auth:check()都工作,Auth:user() - &gt; foo();失败
错误消息
Symfony \ Component \ Debug \ Exception \ FatalErrorException
Call to undefined method Illuminate\Auth\GenericUser::foo()
答案 0 :(得分:3)
确保app/config/auth.php
有'driver' => 'eloquent'
。因此它将使用您的Eloquent用户模型。