postNr = $postNr; $this->related = $related; $profile = getActiveProfile(); // SECURITY: If a profile is active, validate the user has access to it // This prevents session manipulation while allowing public access (null profile) if ($profile) { \App\Helpers\ProfileAuthorizationHelper::authorize($profile); } $location = $profile && $profile->locations ? $profile->locations()->first() : null; if ($location) { // If no division and no city as location set if (!$location->division && !$location->city) { $country = Location::find($location->id)->country; $categoryable_id = $country ? $country->id : null; $categoryable_type = Country::class; // Include also all other countries if $related is set in view if ($related) { $categoryable_id = Country::pluck('id'); } else { $categoryable_id = [$categoryable_id]; } // Division without city is set as location } elseif ($location && $location->division && !$location->city) { $categoryable_id = Location::find($location->id)->division->id; $categoryable_type = Division::class; // Include also all other divisions in the same country if $related is set in view if ($related) { $categoryable_id = Division::find($categoryable_id)->parent->divisions->pluck('id'); } else { $categoryable_id = [$categoryable_id]; } // City is set as location } elseif ($location && $location->city) { $categoryable_id = Location::find($location->id)->city->id; $categoryable_type = City::class; // Include also all other cities in the same division if $related is set in view if ($related) { $categoryable_id = City::find($categoryable_id)->parent->cities->pluck('id'); } else { $categoryable_id = [$categoryable_id]; } } // No matching location is set } else { $categoryable_id = []; $categoryable_type = ''; } $post = Post::with([ 'postable' => function ($query) { $query->select(['id', 'name']); }, 'category' => function ($query) use ($categoryable_id, $categoryable_type) { $query ->where('type', Meeting::class) ->where(function ($query) use ($categoryable_id, $categoryable_type) { $query ->whereIn('categoryable_id', $categoryable_id) ->where('categoryable_type', $categoryable_type); }); }, 'translations' => function ($query) { $query ->where('locale', App::getLocale()) ; }, 'meeting', 'media', ]) ->whereHas('category', function ($query) use ($categoryable_id, $categoryable_type) { $query ->where('type', Meeting::class) ->where(function ($query) use ($categoryable_id, $categoryable_type) { $query ->whereIn('categoryable_id', $categoryable_id) ->where('categoryable_type', $categoryable_type); }); }) ->whereHas('translations', function ($query) { $query ->where('locale', App::getLocale()) ->whereDate('from', '<=', now()) ->where(function ($query) { $query->whereDate('till', '>', now())->orWhereNull('till'); }) ->orderBy('updated_at', 'desc'); }) ->get()->sortBy(function ($query) { if (isset($query->meeting->from)) { return $query->meeting->from; }; })->values(); // Use values() method to reset the collection keys after sortBy $lastNr = $post->count() - 1; if ($postNr > $lastNr) { $post = null; } else { $post = $post[$postNr]; } if (isset($post->translations)) { $this->post = $post->translations->first(); $this->post['start'] = Carbon::parse($post->translations->first()->updated_at)->isoFormat('LL'); $this->post['category'] = Category::find($post->category_id)->translations->where('locale', App::getLocale())->first()->name; $this->post['author'] = $post->postable->name; $this->post['id'] = $post->id; $this->post['model'] = get_class($post); $this->post['like_count'] = $post->like_count ?? 0; if ($post->meeting) { $this->post['address'] = ($post->meeting->address) ? $post->meeting->address : ''; $this->post['from'] = ($post->meeting->from) ? $post->meeting->from : ''; $this->post['venue'] = ($post->meeting->venue) ? $post->meeting->venue : ''; $this->post['city'] = $post->meeting->location?->first()?->city?->translations?->first()?->name ?? ''; $this->post['price'] = ($post->meeting->price) ? tbFormat($post->meeting->price) : ''; } if ($post->media) { $this->media = Post::find($post->id)->getFirstMedia('posts'); } } } public function render() { return view('livewire.main-page.event-card-full'); } }