Reputation: 2203
I'd like to share data with all views via the AppServiceProvider. Moreover, I'd like to check the guard type first to give some specific output for each user type.
I tried to check the guard in AppServiceProvider via Auth::guard('admin')->check()
but it returns false. However, the same if statement works perfectly in my Controllers.
I've also included Illuminate\Support\Facades\Auth
and Illuminate\Support\Facades\View
.
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\View;
class AppServiceProvider extends ServiceProvider
{
public function boot()
{
if (Auth::guard('admin')->check()) {
// Share data with views
}
}
}
The if statement returns false, although I'm logged in as admin.
Upvotes: 0
Views: 1316
Reputation: 3318
you need view composer for this.
<?php
namespace App\Providers;
use Illuminate\Support\ServiceProvider;
use Auth;
use DB;
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* @return void
*/
public function boot()
{
view()->composer('*', function ($view)
{
if (Auth::guard('admin')->check()) {
}
});
}
/**
* Register any application services.
*
* @return void
*/
public function register()
{
//
}
}
If you want to pass data in views.
view()->composer('*', function ($view)
{
if (Auth::guard('admin')->check()) {
$admin = DB::table('admins')->first(); // for example
$view->with(compact('admin'));
}
});
Upvotes: 3