1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Mongi\Mongicommerce\Http\Controllers\admin; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Http\Request; |
8
|
|
|
use Mongi\Mongicommerce\Http\Controllers\Controller; |
9
|
|
|
use Mongi\Mongicommerce\Models\Product; |
10
|
|
|
use Mongi\Mongicommerce\Models\ProductItem; |
11
|
|
|
use Mongi\Mongicommerce\Models\ProductItemDetail; |
12
|
|
|
|
13
|
|
|
class AdminNewProductVariationController extends Controller |
14
|
|
|
{ |
15
|
|
|
public function page($id_product){ |
16
|
|
|
$product = Product::find($id_product); |
17
|
|
|
return view('mongicommerce::admin.pages.products.new_product_variation',['product' => $product]); |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
public function createNewVariation(Request $r){ |
21
|
|
|
$r->validate([ |
22
|
|
|
'category_id' => 'required', |
23
|
|
|
'quantity' => 'required', |
24
|
|
|
'price' => 'required', |
25
|
|
|
'details' => 'required|min:4', |
26
|
|
|
]); |
27
|
|
|
$product_id = $r->get('product_id'); |
28
|
|
|
|
29
|
|
|
$details = json_decode($r->get('details'),true); |
30
|
|
|
$quantity = $r->get('quantity'); |
31
|
|
|
$price = $r->get('price'); |
32
|
|
|
|
33
|
|
|
$product = Product::find($product_id); |
34
|
|
|
|
35
|
|
|
$product_item = new ProductItem(); |
36
|
|
|
$product_item->product_id = $product->id; |
37
|
|
|
$product_item->img = null; |
38
|
|
|
$product_item->price = $price; |
39
|
|
|
$product_item->quantity = $quantity; |
40
|
|
|
$product_item->save(); |
41
|
|
|
|
42
|
|
|
foreach($details as $detail){ |
43
|
|
|
$detail_obj = (object) $detail; |
44
|
|
|
$product_detail = new ProductItemDetail(); |
45
|
|
|
$product_detail->product_item_id = $product_item->id; |
46
|
|
|
$product_detail->product_detail_id = $detail_obj->detail_id; |
47
|
|
|
$product_detail->product_detail_value_id = $detail_obj->detail_value; |
48
|
|
|
$product_detail->save(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
} |
52
|
|
|
} |
53
|
|
|
|