From 236c6dd1d0a0b6a1e2845b876f9660ea991bfd85 Mon Sep 17 00:00:00 2001
From: Wajid Abid Khan
Date: Tue, 2 Jan 2024 08:28:59 +0000
Subject: [PATCH] Fix issue #80,#81: Added pagination and optimize the query.
---
app/Http/Livewire/CollectionPage.php | 15 +++---
resources/views/components/page.blade.php | 52 +++++++++++++++++++
.../views/livewire/collection-page.blade.php | 11 ++--
.../views/livewire/product-page.blade.php | 2 +-
4 files changed, 66 insertions(+), 14 deletions(-)
create mode 100644 resources/views/components/page.blade.php
diff --git a/app/Http/Livewire/CollectionPage.php b/app/Http/Livewire/CollectionPage.php
index 49e8032..650502c 100644
--- a/app/Http/Livewire/CollectionPage.php
+++ b/app/Http/Livewire/CollectionPage.php
@@ -6,12 +6,16 @@
use Livewire\Component;
use Livewire\ComponentConcerns\PerformsRedirects;
use Lunar\Models\Collection;
+use Livewire\WithPagination;
+use Lunar\Models\Url;
class CollectionPage extends Component
{
use PerformsRedirects,
FetchesUrls;
+ use WithPagination;
+ protected $paginationTheme = 'tailwind';
/**
* {@inheritDoc}
*
@@ -25,11 +29,6 @@ public function mount($slug)
$this->url = $this->fetchUrl(
$slug,
Collection::class,
- [
- 'element.thumbnail',
- 'element.products.variants.basePrices',
- 'element.products.defaultUrl',
- ]
);
if (! $this->url) {
@@ -44,7 +43,8 @@ public function mount($slug)
*/
public function getCollectionProperty()
{
- return $this->url->element;
+ $collections = Url::whereElementType(Collection::class)->where('element_id', '=', $this->url->id);
+ return $collections->inRandomOrder()->first()?->element;
}
/**
@@ -52,6 +52,7 @@ public function getCollectionProperty()
*/
public function render()
{
- return view('livewire.collection-page');
+
+ return view('livewire.collection-page', ['products' => $this->collection->products()->paginate(9)]);
}
}
diff --git a/resources/views/components/page.blade.php b/resources/views/components/page.blade.php
new file mode 100644
index 0000000..0f8f00f
--- /dev/null
+++ b/resources/views/components/page.blade.php
@@ -0,0 +1,52 @@
+@if ($paginator->hasPages())
+
+@endif
diff --git a/resources/views/livewire/collection-page.blade.php b/resources/views/livewire/collection-page.blade.php
index a2d67b3..9ddec8a 100644
--- a/resources/views/livewire/collection-page.blade.php
+++ b/resources/views/livewire/collection-page.blade.php
@@ -3,12 +3,11 @@
{{ $this->collection->translateAttribute('name') }}
-
- @forelse($this->collection->products as $product)
-
- @empty
- @endforelse
-
+ @foreach ($products as $product)
+
+ @endforeach
+
+ {{ $products->links('components.page') }}
diff --git a/resources/views/livewire/product-page.blade.php b/resources/views/livewire/product-page.blade.php
index a2ce52a..a04d30e 100644
--- a/resources/views/livewire/product-page.blade.php
+++ b/resources/views/livewire/product-page.blade.php
@@ -38,7 +38,7 @@ class="object-cover rounded-xl"
- {{ $this->product->translateAttribute('description') }}
+ {!! $this->product->translateAttribute('description') !!}