*/ public function share(Request $request): array { /** @var User|null $user */ $user = $request->user(); return array_merge(parent::share($request), [ 'auth' => [ 'user' => $user ? $this->serializeUser($user) : null, 'permissions' => fn () => $user ? $user->role->cachedPermissions()->pluck('name')->all() : [], 'impersonating' => app('impersonate')->isImpersonating(), ], 'settings' => $this->settings(), 'config' => [ // Drives the "Active Sessions" UI, which only works on the // database session driver. 'databaseSessions' => config('session.driver') === 'database', 'dateFormat' => config('app.date_format'), 'dateTimeFormat' => config('app.date_time_format'), ], 'flash' => [ 'success' => $request->session()->get('success'), 'error' => $request->session()->get('error'), ], 'locale' => app()->getLocale(), 'locales' => collect(Locale::AVAILABLE_LOCALES) ->map(fn (string $locale) => [ 'code' => $locale, 'flag' => Locale::flagUrl($locale), ])->all(), 'translations' => fn () => Translations::catalog(), 'vanguard' => [ 'sidebar' => fn () => $user ? $this->sidebar($user) : [], 'slots' => fn () => [ 'navbar:items' => Vanguard::resolveSlot('navbar:items'), 'navbar:dropdown' => Vanguard::resolveSlot('navbar:dropdown'), ], ], ]); } /** * The authenticated user, including the presenter derived fields that the * Blade layer used to read through $user->present(). * * @return array */ private function serializeUser(User $user): array { return [ 'id' => $user->id, 'email' => $user->email, 'username' => $user->username, 'first_name' => $user->first_name, 'last_name' => $user->last_name, 'status' => $user->status, 'name' => $user->present()->name, 'nameOrEmail' => $user->present()->nameOrEmail, 'avatar' => $user->present()->avatar, 'lastLogin' => $user->present()->lastLogin, ]; } /** * @return array */ private function settings(): array { $settings = []; foreach (self::SHARED_SETTINGS as $key) { $settings[$key] = setting($key); } return $settings; } /** * The authorised sidebar tree contributed by every registered plugin. * * @return array */ private function sidebar(User $user): array { return collect(Vanguard::availablePlugins()) ->map(fn (Plugin $plugin) => $plugin->sidebar()) ->filter(fn (?Item $item) => $item !== null && $item->authorize($user)) ->map(fn (Item $item) => $item->toArray($user)) ->values() ->all(); } }