Laravel 5.4 : Get logged in user id inside __construct()

后端 未结 3 1751
有刺的猬
有刺的猬 2020-12-03 23:59

I am trying to access Auth::user()->id; inside constructor but it always return the error Trying to get property of non-object. I study in t

3条回答
  •  无人及你
    2020-12-04 00:19

    To solve this problem App\Providers\AppServiceProvider was indeed my first guess. This would work mostly but with an exception to access Session data.

    So If you try to access Session data within boot method of AppServiceProvider you will get null. So to do this so it works perfectly good in any case Middleware is a good option. You can write this logic of sharing a variable to all your desired views. Just create a middleware and include that in your route or routegroup .

提交回复
热议问题