-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathWishlistController.php
More file actions
50 lines (38 loc) · 1.34 KB
/
WishlistController.php
File metadata and controls
50 lines (38 loc) · 1.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
<?php
namespace App\Http\Controllers\Store;
use App\Http\Controllers\Controller;
use App\Models\Wishlist;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
class WishlistController extends Controller
{
public function index()
{
$customer = Auth::guard('customer')->user();
$products = $customer->wishlistProducts()
->with(['translation', 'thumbnail', 'primaryVariant', 'reviews'])
->withCount('reviews')
->orderBy('wishlists.created_at', 'desc')
->get();
return view('wishlist.index', compact('products'));
}
public function toggle(Request $request)
{
$request->validate([
'product_id' => 'required|exists:products,id',
]);
$customer = Auth::guard('customer')->user();
$wishlist = Wishlist::where('customer_id', $customer->id)
->where('product_id', $request->product_id)
->first();
if ($wishlist) {
$wishlist->delete();
return response()->json(['status' => 'removed', 'message' => 'Removed from favorites']);
}
Wishlist::create([
'customer_id' => $customer->id,
'product_id' => $request->product_id,
]);
return response()->json(['status' => 'added', 'message' => 'Added to favorites']);
}
}