|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Adminetic\Website\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Adminetic\Website\Contracts\ProductRepositoryInterface; |
|
6
|
|
|
use Adminetic\Website\Http\Requests\ProductRequest; |
|
7
|
|
|
use Adminetic\Website\Models\Admin\Product; |
|
8
|
|
|
use Illuminate\Support\Facades\Cache; |
|
9
|
|
|
|
|
10
|
|
|
class ProductRepository implements ProductRepositoryInterface |
|
11
|
|
|
{ |
|
12
|
|
|
// Product Index |
|
13
|
|
|
public function indexProduct() |
|
14
|
|
|
{ |
|
15
|
|
|
$products = config('adminetic.caching', true) |
|
16
|
|
|
? (Cache::has('products') ? Cache::get('products') : Cache::rememberForever('products', function () { |
|
17
|
|
|
return Product::orderBy('position')->get(); |
|
18
|
|
|
})) |
|
19
|
|
|
: Product::orderBy('position')->get(); |
|
20
|
|
|
|
|
21
|
|
|
return compact('products'); |
|
22
|
|
|
} |
|
23
|
|
|
|
|
24
|
|
|
// Product Create |
|
25
|
|
|
public function createProduct() |
|
26
|
|
|
{ |
|
27
|
|
|
// |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
// Product Store |
|
31
|
|
|
public function storeProduct(ProductRequest $request) |
|
32
|
|
|
{ |
|
33
|
|
|
$product = Product::create($request->validated()); |
|
34
|
|
|
$this->syncAttribute($product); |
|
35
|
|
|
$this->uploadImage($product); |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
// Product Show |
|
39
|
|
|
public function showProduct(Product $product) |
|
40
|
|
|
{ |
|
41
|
|
|
return compact('product'); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
// Product Edit |
|
45
|
|
|
public function editProduct(Product $product) |
|
46
|
|
|
{ |
|
47
|
|
|
return compact('product'); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// Product Update |
|
51
|
|
|
public function updateProduct(ProductRequest $request, Product $product) |
|
52
|
|
|
{ |
|
53
|
|
|
$product->update($request->validated()); |
|
54
|
|
|
$this->syncAttribute($product); |
|
55
|
|
|
$this->uploadImage($product); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
// Product Destroy |
|
59
|
|
|
public function destroyProduct(Product $product) |
|
60
|
|
|
{ |
|
61
|
|
|
$product->delete(); |
|
62
|
|
|
} |
|
63
|
|
|
|
|
64
|
|
|
// Upload Image |
|
65
|
|
|
private function uploadImage(Product $product) |
|
66
|
|
|
{ |
|
67
|
|
|
if (request()->has('image')) { |
|
68
|
|
|
$product |
|
69
|
|
|
->addFromMediaLibraryRequest(request()->image) |
|
70
|
|
|
->toMediaCollection('image'); |
|
71
|
|
|
} |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
// Sync Attribute |
|
75
|
|
|
private function syncAttribute(Product $product) |
|
76
|
|
|
{ |
|
77
|
|
|
if (request()->has('product_attributes')) { |
|
78
|
|
|
$product->attributes()->detach(); |
|
79
|
|
|
foreach (request()->product_attributes as $attribute_id => $values) { |
|
80
|
|
|
$product->attributes()->attach($attribute_id, [ |
|
81
|
|
|
'values' => json_encode($values), |
|
82
|
|
|
]); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
} |
|
87
|
|
|
|