在我的Laravel 5.2中。在構造函數中public function __construct(){ $this->data['user'] = Auth::guard('customer')->user(); $this->middleware('customer');}因此,我可以在控制器中的任何位置使用“$this->data['user']”。但在Laravel 5.3或更高版本中,我們無法訪問構造函數中經過身份驗證的用戶。當我嘗試在構造函數中使用基于閉包的中間件時。public function __construct(){ $this->data['user'] = Auth::guard('customer')->user(); $this->middleware('customer');}然后我收到錯誤:錯誤異常 嘗試獲取非對象 http://localhost/dhruv/intranet-v6.0/public/profile 的屬性“標頭”
1 回答

回首憶惘然
TA貢獻1847條經驗 獲得超11個贊
試試這個方式
public function __construct()
{
$this->middleware('customer');
$this->middleware(function ($request, $next) {
$this->data['user'] = Auth::guard('customer')->user();
return $next($request);
});
}
- 1 回答
- 0 關注
- 78 瀏覽
添加回答
舉報
0/150
提交
取消