2 回答

TA貢獻1840條經驗 獲得超5個贊
auth()->user()當您使用或關系(已加載/設置)時,它不會進行多次調用。您可以安裝發條并檢查它。
不過,我不會在 User 類之外進行這些比較。將這些方法添加到您的用戶模型中。
public function isAdmin()
{
return $this->role === 'admin';
}
public function isSupervised()
{
return $this->role === 'supervised';
}
并像這樣使用它們;
auth()->user()->isAdmin()

TA貢獻1772條經驗 獲得超5個贊
我會使用第一種方法,而不是創建不必要的變量。它不會多次調用數據庫。查看user()中的方法SessionGaurd.php
// If we've already retrieved the user for the current request we can just
// return it back immediately. We do not want to fetch the user data on
// every call to this method because that would be tremendously slow.
if (! is_null($this->user)) {
return $this->user;
}
- 2 回答
- 0 關注
- 134 瀏覽
添加回答
舉報