middleware(function (Request $request, callable $next) { if (Auth::guard('club')->check()) { /** @var \App\Models\Customer */ $customerModel = Auth::guard('club')->user(); $notificationModels = $customerModel->getAllNotifications(); View::share( 'notifications_count', $notificationModels->count() - $customerModel->readNotifications()->count() ); View::share('notifications_list', $notificationModels); View::share('notifications_message_count', $notificationModels // @phpstan-ignore-next-line ->whereNotIn('id', $customerModel->readNotifications->pluck('id')) ->filter(function (Notification $notificationModel) { return $notificationModel->notifiable_type === Message::class and !is_null($notificationModel->customer_id); }) ->count()); $discountCache = Cache::remember( "discountsCustomerCount_{$customerModel->hashcode}", 86400, function () use ($customerModel) { return Discount::getCustomerDiscountsCount($customerModel->hashcode); } ); View::share('discountsCustomerCount', $discountCache); } //end if return $next($request); }); } /** * Gives the redirection path for FO Auth controllers. * * @return string */ protected function authLoggedRedirectPath(): string { return route('club.home.show'); } /** * Get a paginate resource. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\JsonResponse */ public function jsonPaginateModels(Request $request): \Illuminate\Http\JsonResponse { /** @var \Illuminate\Database\Eloquent\Model $models */ $models = $request->targetModel::query() ->where($request->input('fieldName') ?? 'name', 'like', "%{$request->input('search')}%") ->orderBy($request->input('fieldName') ?? 'name', 'ASC') ->paginate($request->input('paginate') ?? 10); return \response()->json($models); } /** * Get models titles for search input propositions. * * @param Builder $query * @param string $searchField * @param string|null $search * @return \Illuminate\Pagination\LengthAwarePaginator */ public function getModelsTitles( Builder $query, string $searchField, string|null $search = null ): \Illuminate\Pagination\LengthAwarePaginator { $modelsTitles = []; $searchLower = Str::lower($search); $query->when(!is_null($search), function (Builder $q) use ($searchLower, $searchField) { $q->where($searchField, 'like', "%{$searchLower}%"); }) ->where('published', true) ->inRandomOrder() ->pluck($searchField) ->each(function (string $title) use (&$modelsTitles, $searchLower) { if (count($modelsTitles) < 5) { foreach (explode(" ", $title) as $word) { $word = Str::lower($word); if ( strlen($word) > 3 && !in_array($word, $modelsTitles) && str_contains($word, $searchLower) ) { $modelsTitles[] = $word; } } return $modelsTitles; } }) ->toArray(); \sort($modelsTitles); $titles = array_slice($modelsTitles, 0, 5); return new LengthAwarePaginatorCustom($titles, count($titles), (count($titles) <= 0) ? 1 : count($titles)); } }