-
Notifications
You must be signed in to change notification settings - Fork 71
Expand file tree
/
Copy pathShopController.php
More file actions
71 lines (63 loc) · 2.97 KB
/
ShopController.php
File metadata and controls
71 lines (63 loc) · 2.97 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
<?php
namespace App\Http\Controllers\Store;
use App\Http\Controllers\Controller;
use App\Models\Brand;
use App\Models\Category;
use App\Models\Product;
use Illuminate\Http\Request;
class ShopController extends Controller
{
public function index(Request $request)
{
$locale = app()->getLocale();
$filters = [
'category' => $request->input('category', []),
'brand' => $request->input('brand', []),
'price_min' => $request->input('price_min', 0),
'price_max' => $request->input('price_max', 1000),
'color' => $request->input('color', []),
'size' => $request->input('size', []),
];
$products = Product::with(['translation', 'variants.attributeValues'])
->withCount('reviews')
->withAvg('reviews', 'rating')
->when(! empty($filters['category']), function ($query) use ($filters) {
$query->whereIn('category_id', $filters['category']);
})
->when(! empty($filters['brand']), function ($query) use ($filters) {
$query->whereIn('brand_id', $filters['brand']);
})
->whereHas('variants', function ($variantQuery) use ($filters) {
$variantQuery
->when($filters['price_min'], function ($q) use ($filters) {
$q->where('price', '>=', $filters['price_min']);
})
->when($filters['price_max'], function ($q) use ($filters) {
$q->where('price', '<=', $filters['price_max']);
})
->when(! empty($filters['color']), function ($q) use ($filters) {
$q->whereHas('attributeValues', function ($avQuery) use ($filters) {
$avQuery->whereIn('value', $filters['color'])
->whereHas('attribute', function ($aQuery) {
$aQuery->where('name', 'Color');
});
});
})
->when(! empty($filters['size']), function ($q) use ($filters) {
$q->whereHas('attributeValues', function ($avQuery) use ($filters) {
$avQuery->whereIn('value', $filters['size'])
->whereHas('attribute', function ($aQuery) {
$aQuery->where('name', 'Size');
});
});
});
})
->paginate(12);
$brands = Brand::with('translation')->withCount('products')->get();
$categories = Category::with('translation')->withCount('products')->get();
if ($request->ajax()) {
return view('themes.xylo.partials.product-list', compact('products'))->render();
}
return view('themes.xylo.shop', compact('products', 'categories', 'brands'));
}
}