在构造方法中使用 Auth
Use Auth in construct method
Laravel 版本: 5.6.*
PHP版本:7.2.2
数据库驱动程序和版本: Mysql 5.0.12
描述:
我需要 return 构造方法中的登录用户
重现步骤:
我需要 return 构造方法中的登录用户,
public function __construct(){
$this->menu = new Menu();
$this->menu->getEnabledMenu();
$this->categories = $this->
menu::with('children')->
where('parent_id','=',0)->
whereIn('id', [8,9,7])->
orderBy('position', 'asc')->
get();
}
我试过 $this->user->Auth::user()->id;但这不起作用
我也试过这个
https://laravel-news.com/controller-construct-session-changes-in-laravel-5-3
$this->middleware(function ($request, $next){
$this->user= Auth::user();
return $next($request);
});
但我不知道如何访问 $this->user
请
我需要帮助
对不起我的英语
您需要在控制器上定义 $user
属性,例如:
class MyController {
// keep a reference to the authenticated user.
public $user;
public function __construct() {
$this->middleware(function ($request, $next){
$this->user = Auth::user();
return $next($request);
});
}
...
}
Laravel 版本: 5.6.*
PHP版本:7.2.2
数据库驱动程序和版本: Mysql 5.0.12
描述:
我需要 return 构造方法中的登录用户
重现步骤: 我需要 return 构造方法中的登录用户,
public function __construct(){
$this->menu = new Menu();
$this->menu->getEnabledMenu();
$this->categories = $this->
menu::with('children')->
where('parent_id','=',0)->
whereIn('id', [8,9,7])->
orderBy('position', 'asc')->
get();
}
我试过 $this->user->Auth::user()->id;但这不起作用
我也试过这个
https://laravel-news.com/controller-construct-session-changes-in-laravel-5-3
$this->middleware(function ($request, $next){
$this->user= Auth::user();
return $next($request);
});
但我不知道如何访问 $this->user
请 我需要帮助
对不起我的英语
您需要在控制器上定义 $user
属性,例如:
class MyController {
// keep a reference to the authenticated user.
public $user;
public function __construct() {
$this->middleware(function ($request, $next){
$this->user = Auth::user();
return $next($request);
});
}
...
}