在 Laravel 服务提供者中使用 auth [英] using auth in laravel service provider
本文介绍了在 Laravel 服务提供者中使用 auth的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
每当我尝试使用 Auth::User()
我得到非对象属性,因为我的 Auth::guest()
在我使用它们时返回 true服务提供商
whenever i tried to use Auth::User()
i am getting non object property because my Auth::guest()
returns true whenever i use them in service provider
use IlluminateContractsViewView;
use IlluminateSupportServiceProvider;
use IlluminateContractsViewFactory;
use App
elations;
use AppUser;
use DB;
use IlluminateSupportFacadesAuth;
class RelationServiceProvider extends ServiceProvider
{
/**
* Bootstrap the application services.
*
* @return void
*/
public function boot()
{
//
Auth::User()->id;
$relation_friend_for_logged_user = DB::select( DB::raw("SELECT * FROM users"));
$value = "asd";
// for injecting objct
View()->share('count', $value);
}
但是为什么 Auth::guest()
无论我是否登录都返回 true
but why Auth::guest()
is returning true whether i am logged in
推荐答案
您可能想要使用 View Composer 为此.据我所知,经过身份验证的用户在您的服务提供商启动方法中尚不可用.
You probably want to use a View Composer for this. As far as I know the authenticated user is not yet available in your service providers boot method.
public function boot(Guard $auth) {
view()->composer('*', function($view) use ($auth) {
// get the current user
$currentUser = $auth->user();
// do stuff with the current user
// ...
// pass the data to the view
$view->with('currentUser', $currentUser);
});
}
这篇关于在 Laravel 服务提供者中使用 auth的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文