我试图在laravel 5.1中使用auth()->login()
,但它会返回错误。请参阅下面的代码:
$user = User::where('username', $username)->where('activation_code', $activation_code);
$not_activated_user = $user->where('status', 0)->where('confirmed', 0);
if($not_activated_user->count() == 1){
$not_activated_user->update([
'status' => 1,
'confirmed' => 1
]);
auth()->login($user->where('status', 1)->where('confirmed', 1));
}
我还在我的use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
模型中导入implements AuthenticatableContract
和User
,但它仍会返回相同的错误。这是为什么?我还尝试在->get()
中使用->login(....->get())
来获取当前用户,但仍然是同样的错误。
错误:
Argument 1 passed to Illuminate\Auth\Guard::login() must implement interface Illuminate\Contracts\Auth\Authenticatable, instance of Illuminate\Database\Eloquent\Builder given
答案 0 :(得分:0)
尝试
$user = User::where('username', $username)->where('activation_code', $activation_code)->firstOrFail();
$user->update([
'status' => 1,
'confirmed' => 1
]);
auth()->login($user);
}
答案 1 :(得分:0)
现在正在使用,我再次调用User
模型来选择当前用户:)