type = $type; $this->fallbackTitle = $fallbackTitle; $this->fallbackDescription = $fallbackDescription; if ($sticky) { $this->sticky = true; } if ($random) { $this->random = true; } if ($latest) { $this->latest = true; } } public function render() { // Sticky post if ($this->sticky) { $locale = App::getLocale(); $posts = Post::with([ 'category', 'images' => function ($query) { $query->select('images.id', 'caption', 'path'); }, 'translations' => function ($query) use ($locale) { $query->where('locale', $locale) ->orderBy('created_at', 'desc') ->limit(3); } ]) ->whereHas('category', function ($query) { $query->where('type', $this->type); }) ->whereHas('translations', function ($query) use ($locale) { $query->where('locale', $locale) ->whereDate('from', '<=', now()) ->where(function ($query) { $query->whereDate('till', '>', now())->orWhereNull('till'); }) ->orderBy('updated_at', 'desc'); }) ->orderBy('created_at', 'desc') ->limit(3) ->first(); } // Random post if ($this->random) { $locale = App::getLocale(); $posts = Post::with([ 'category', 'translations' => function ($query) use ($locale) { $query->where('locale', $locale) ->orderBy('created_at', 'desc') ->limit(1); } ]) ->whereHas('category', function ($query) { $query->where('type', $this->type); }) ->whereHas('translations', function ($query) use ($locale) { $query->where('locale', $locale) ->whereDate('from', '<=', now()) ->where(function ($query) { $query->whereDate('till', '>', now())->orWhereNull('till'); }) ->orderBy('updated_at', 'desc'); }) ->inRandomOrder() // This replaces the orderBy() method ->first(); } // Latest post if ($this->latest) { $locale = App::getLocale(); $posts = Post::with([ 'category', 'translations' => function ($query) use ($locale) { $query->where('locale', $locale) ->orderBy('created_at', 'desc') ->limit(1); } ]) ->whereHas('category', function ($query) { $query->where('type', $this->type); }) ->whereHas('translations', function ($query) use ($locale) { $query->where('locale', $locale) ->whereDate('from', '<=', now()) ->where(function ($query) { $query->whereDate('till', '>', now())->orWhereNull('till'); }) ->orderBy('updated_at', 'desc'); }) ->orderBy('created_at', 'desc') ->first(); } $image = null; if ($posts && $posts->hasMedia('*')) { $image = $posts->getFirstMediaUrl('*', 'half_hero'); } return view('livewire.main-post', [ 'posts' => $posts, 'image' => $image, ]); } }