From f2c2cf11edf61dc7a71331dcfd21daa8a6a3acb3 Mon Sep 17 00:00:00 2001 From: Bayu Lukman Yusuf Date: Fri, 27 Feb 2026 16:19:51 +0700 Subject: [PATCH] wishlist --- app/Http/Controllers/WishController.php | 84 ++++++ app/Models/ItemReference.php | 2 + app/Models/Items.php | 6 + app/Models/Wishlist.php | 27 ++ .../Member/WishlistRepository.php | 75 ++++++ resources/views/account/wishlist.blade.php | 240 +----------------- .../components/home/product-card.blade.php | 4 +- .../partials/account-sidebar.blade.php | 2 +- .../views/shop/product-fashion.blade.php | 166 ++++++++++-- routes/web.php | 10 + 10 files changed, 358 insertions(+), 258 deletions(-) create mode 100644 app/Http/Controllers/WishController.php create mode 100644 app/Models/Wishlist.php create mode 100644 app/Repositories/Member/WishlistRepository.php diff --git a/app/Http/Controllers/WishController.php b/app/Http/Controllers/WishController.php new file mode 100644 index 0000000..8295ddf --- /dev/null +++ b/app/Http/Controllers/WishController.php @@ -0,0 +1,84 @@ +wishlistRepository = $wishlistRepository; + } + + /** + * Display the wishlist page + */ + public function index(Request $request) + { + try { + $wishlists = $this->wishlistRepository->getList($request); + + return view('account.wishlist', [ + 'wishlists' => $wishlists, + 'user' => Auth::user() + ]); + } catch (\Exception $e) { + return redirect()->back()->with('error', 'Failed to load wishlist: ' . $e->getMessage()); + } + } + + /** + * Add item to wishlist + */ + public function store(Request $request): JsonResponse + { + $request->validate([ + 'item_id' => 'required|exists:items,id' + ]); + + try { + $wishlist = $this->wishlistRepository->create([ + 'item_id' => $request->item_id + ]); + + return response()->json([ + 'success' => true, + 'message' => 'Item added to wishlist successfully', + 'wishlist' => $wishlist + ]); + } catch (\Exception $e) { + return response()->json([ + 'success' => false, + 'message' => 'Failed to add item to wishlist: ' . $e->getMessage() + ], 500); + } + } + + /** + * Remove item from wishlist + */ + public function destroy(Request $request, $id): JsonResponse + { + try { + + + $this->wishlistRepository->delete($id); + + return response()->json([ + 'success' => true, + 'message' => 'Item removed from wishlist successfully' + ]); + } catch (\Exception $e) { + return response()->json([ + 'success' => false, + 'message' => 'Failed to remove item from wishlist: ' . $e->getMessage() + ], 500); + } + } +} diff --git a/app/Models/ItemReference.php b/app/Models/ItemReference.php index 4853b09..5c2a1b6 100644 --- a/app/Models/ItemReference.php +++ b/app/Models/ItemReference.php @@ -199,4 +199,6 @@ class ItemReference extends Model return $result; } + + } diff --git a/app/Models/Items.php b/app/Models/Items.php index 06e3e2c..18b1be4 100644 --- a/app/Models/Items.php +++ b/app/Models/Items.php @@ -355,4 +355,10 @@ class Items extends Model return 0; } } + + + public function isWishlist() : bool + { + return $this->hasOne(Wishlist::class, 'item_id', 'id')->where('customer_id', auth()->user()->customer->id)->exists(); + } } diff --git a/app/Models/Wishlist.php b/app/Models/Wishlist.php new file mode 100644 index 0000000..56a69c2 --- /dev/null +++ b/app/Models/Wishlist.php @@ -0,0 +1,27 @@ +belongsTo(Customer::class); + } + + public function item(): BelongsTo + { + return $this->belongsTo(Items::class); + } +} diff --git a/app/Repositories/Member/WishlistRepository.php b/app/Repositories/Member/WishlistRepository.php new file mode 100644 index 0000000..83e48a2 --- /dev/null +++ b/app/Repositories/Member/WishlistRepository.php @@ -0,0 +1,75 @@ +user()->id)->first(); + + if (!$customer) { + throw new \Exception('Customer not found'); + } + $limit = 20; + + $wishlist = Wishlist::where('customer_id', $customer->id) + ->with([ + 'item.variants', + + ]) + ->orderBy('created_at', 'desc') + ->paginate($limit); + + return $wishlist; + } + + public function create($data) + { + $model = DB::transaction(function () use ($data) { + $customer = Customer::where('user_id', auth()->user()->id)->first(); + + // Check if item already exists in wishlist + $existing = Wishlist::where('customer_id', $customer->id) + ->where('item_id', $data['item_id']) + ->first(); + + if ($existing) { + return $existing; // Return existing item if already in wishlist + } + + $model = Wishlist::create([ + 'customer_id' => $customer->id, + 'item_id' => $data['item_id'], + ]); + + return $model; + }); + + + return $model; + } + + public function delete($item_id) + { + $wishlist = DB::transaction(function () use ($item_id) { + $wishlist = Wishlist::where('customer_id', auth()->user()->customer->id) + ->where('item_id', $item_id) + ->first(); + + if (!$wishlist) { + throw new \Exception('Wishlist not found'); + } + + $wishlist->delete(); + return $wishlist; + }); + + + return $wishlist; + } +} diff --git a/resources/views/account/wishlist.blade.php b/resources/views/account/wishlist.blade.php index 0531122..8761d6f 100644 --- a/resources/views/account/wishlist.blade.php +++ b/resources/views/account/wishlist.blade.php @@ -7,15 +7,15 @@

Wishlist

- --}}
-
+ {{--
Interesting offers
@@ -69,9 +69,9 @@
-
+
--}} - --}}
- -
-
-
-
-
- -
-
- - -21% -
- VR Glasses -
-
-
-
-
-
- - - - - -
- (123) -
-

- - VRB01 Virtual Reality Glasses - -

-
-
$340.99 $430.00
- -
-
-
-
- -
-
-
-
-
- -
-
- -
- iPhone 14 -
-
-
-
-
-
- - - - - -
- (142) -
-

- - Apple iPhone 14 128GB White - -

-
-
$899.00
- -
-
-
-
- -
-
-
-
-
- -
-
- -
- Smart Watch -
-
-
-
-
-
- - - - - -
- (67) -
-

- - Smart Watch Series 7, White - -

-
-
$429.00
- -
-
-
-
- -
-
-
-
-
- -
-
- -
- iPad Air -
-
-
-
-
-
- - - - - -
- (12) -
-

- - Tablet Apple iPad Air M1 - -

-
-
$540.00
- -
-
-
-
- -
-
-
-
-
- -
-
- -
- AirPods 2 -
-
-
-
-
-
- - - - - -
- (78) -
-

- - Headphones Apple AirPods 2 Pro - -

-
-
$224.00
- -
-
-
-
+ + @foreach ($wishlists as $key => $value) + @include('components.home.product-card', ['product' => $value->item]) + @endforeach +
diff --git a/resources/views/components/home/product-card.blade.php b/resources/views/components/home/product-card.blade.php index dee9213..c765aa5 100644 --- a/resources/views/components/home/product-card.blade.php +++ b/resources/views/components/home/product-card.blade.php @@ -33,8 +33,8 @@ @endif
- @if ($product->variants->count() > 1) -
+{{ $product->variants->count() - 1 }} Varian
+ @if (($product->variants?->count() ?? 0) > 1) +
+{{ ($product->variants->count() ?? 0) - 1 }} Varian
@endif {{--
+1 color
--}} {{--
diff --git a/resources/views/layouts/partials/account-sidebar.blade.php b/resources/views/layouts/partials/account-sidebar.blade.php index ad3e980..3de21cb 100644 --- a/resources/views/layouts/partials/account-sidebar.blade.php +++ b/resources/views/layouts/partials/account-sidebar.blade.php @@ -51,7 +51,7 @@ {{-- 1 --}} + href="{{ route('wishlist.index') }}"> {{ __('account_sidebar.wishlist') }} diff --git a/resources/views/shop/product-fashion.blade.php b/resources/views/shop/product-fashion.blade.php index a373ea9..7d15262 100644 --- a/resources/views/shop/product-fashion.blade.php +++ b/resources/views/shop/product-fashion.blade.php @@ -1,9 +1,6 @@ @extends('layouts.landing', ['title' => $product->name ?? 'Detail']) @section('content') - - -
@@ -73,9 +70,9 @@
@@ -98,17 +95,35 @@ --}} -

{{ $product->name }}

+
+
+

{{ $product->name }}

+
+ + {{-- wishlist/love button --}} + +
{{-- add category --}}
- {{ $product->category->name }} - - {{ $product->brand->name }} -
+ {{ $product->category->name }} + + {{ $product->brand->name }} + + +

{!! nl2br(Str::limit($product->description, 500)) !!}

@@ -144,8 +159,8 @@ @foreach ($product->variants as $key => $variant) + value="{{ $variant->reference->id }}" id="variant-id-{{ $variant->id }}" + {{ $key == 0 ? 'checked' : '' }}>