2016-05-16 118 views
2

我不確定這是否是真正導致我的問題,但也許有人會知道。當我使用Laravel社會名流和去:

$social_user = Socialite::driver($provider)->user(); 

然後在其他地方在我的代碼是這樣:

if ($authUser = User::where('provider_id', $social_user->id)) 
     return $authUser; 

對於一些瘋狂的原因,我得到這樣一個錯誤:通過

參數1以照亮\ Auth \ SessionGuard :: login()必須實現接口Illuminate \ Contracts \ Auth \ Authenticatable,實例Illuminate \ Database \ Eloquent \ Builder給出

但是,如果我d O此我沒有得到一個錯誤:

if($authUser = User::where('email', $social_user->email)->first()) 
     return $authUser; 

我喜歡這個登錄該用戶:

Auth::login($authUser, true); 

有誰知道爲什麼嗎?我正在使用laravel 5.2

回答

1

錯誤信息實際上是自我解釋的。當您執行User::where('provider_id', $social_user->id)時,最終會生成構建器對象,該對象實現了Illuminate\Database\Eloquent\Builder。你可以調用它->get()得到收集結果的(對象的集合,在你的情況下實現了Illuminate\Contracts\Auth\Authenticatable,這樣你就可以在它們之間迭代),或者像你一樣,你可以用->first()得到第一場比賽(一個實現Illuminate\Contracts\Auth\Authenticatable的對象)。您可以在Eloquent documentation中閱讀更多內容。

主要的一點是,直到調用->get()->first()您與生成器對象工作。實際上還有->find()方法,即用於通過pk檢索記錄,您不能使用它通過約束(where)來搜索記錄,但它也會返回模型對象。

+0

謝謝。 :)解決了它。 – Magearlik

相關問題