diff --git a/app/Http/Controllers/Admin/ProductsController.php b/app/Http/Controllers/Admin/ProductsController.php index 314babf9..d7ea1f68 100644 --- a/app/Http/Controllers/Admin/ProductsController.php +++ b/app/Http/Controllers/Admin/ProductsController.php @@ -48,9 +48,11 @@ class ProductsController extends Controller return view('admin.pages.products.form.form', $data); } - public function update(Request $request, $id) + public function update(Request $request, Product $product) { - // + $product = (new ProductService)->update($product, $request->all()); + + return redirect()->route('admin.products.index')->with('success', 'product updated'); } public function destroy($id) diff --git a/app/Models/Product.php b/app/Models/Product.php index ebf05e31..e1e43679 100644 --- a/app/Models/Product.php +++ b/app/Models/Product.php @@ -9,15 +9,15 @@ class Product extends Model { use HasFactory; - protected $fillable = ['image', 'video', 'sort_order', 'status', 'variable']; + protected $fillable = ['image', 'video', 'position', 'active', 'variables']; public function skus() { return $this->hasMany(ProductSku::class); } - public function getVariableDecodedAttribute() + public function getVariablesDecodedAttribute() { - return json_decode($this->variable, true); + return json_decode($this->variables, true); } } diff --git a/app/Models/ProductSku.php b/app/Models/ProductSku.php index 167e78e2..2f61ab08 100644 --- a/app/Models/ProductSku.php +++ b/app/Models/ProductSku.php @@ -9,5 +9,9 @@ class ProductSku extends Model { use HasFactory; - protected $fillable = ['product_id', 'image', 'model', 'sku', 'price', 'quantity', 'is_default']; + protected $fillable = ['product_id', 'variants', 'position', 'image', 'model', 'sku', 'price', 'origin_price', 'cost_price', 'quantity', 'is_default']; + + protected $casts = [ + 'variants' => 'array', + ]; } diff --git a/app/Services/ProductService.php b/app/Services/ProductService.php index d46d9bf2..48901bd8 100644 --- a/app/Services/ProductService.php +++ b/app/Services/ProductService.php @@ -7,24 +7,35 @@ use Illuminate\Support\Facades\DB; class ProductService { - public function create($data) + public function create(array $data): Product { + $product = new Product; + return $this->createOrUpdate($product, $data); + } + + public function update(Product $product, array $data): Product + { + return $this->createOrUpdate($product, $data); + } + + protected function createOrUpdate(Product $product, array $data): Product + { + $isUpdating = $product->id > 0; + try { DB::beginTransaction(); - $product = new Product($data); - if (isset($data['variant'])) { - $product->variable = json_encode($data['variant']); - } + $product->fill($data); $product->saveOrFail(); $skus = []; foreach ($data['skus'] as $index => $rawSku) { - $sku = $rawSku; - $sku['is_default'] = $index == 0; - $skus[] = $sku; + $skus[] = $rawSku; } + if ($isUpdating) { + $product->skus()->delete(); + } $product->skus()->createMany($skus); DB::commit(); diff --git a/database/migrations/2021_12_26_111435_create_tables.php b/database/migrations/2021_12_26_111435_create_tables.php index 175e08b7..c2869adc 100644 --- a/database/migrations/2021_12_26_111435_create_tables.php +++ b/database/migrations/2021_12_26_111435_create_tables.php @@ -15,11 +15,11 @@ class CreateTables extends Migration { Schema::create('products', function (Blueprint $table) { $table->id()->startingValue(100_000); - $table->string('image'); - $table->string('video'); - $table->integer('sort_order'); - $table->boolean('status'); - $table->json('variable'); + $table->string('image')->default(''); + $table->string('video')->default(''); + $table->integer('position')->default(0); + $table->boolean('active'); + $table->json('variables')->nullable(); $table->timestamps(); $table->softDeletes(); }); @@ -27,11 +27,15 @@ class CreateTables extends Migration Schema::create('product_skus', function (Blueprint $table) { $table->id()->startingValue(100_000); $table->unsignedBigInteger('product_id'); - $table->string('image'); - $table->string('model'); - $table->string('sku'); - $table->double('price'); - $table->integer('quantity'); + $table->string('variants')->default(0); + $table->integer('position')->default(0); + $table->string('image')->default(''); + $table->string('model')->default(''); + $table->string('sku')->default(''); + $table->double('price')->default(0); + $table->double('origin_price')->default(0); + $table->double('cost_price')->default(0); + $table->integer('quantity')->default(0); $table->boolean('is_default'); $table->timestamps(); }); diff --git a/resources/views/admin/pages/products/form/form.blade.php b/resources/views/admin/pages/products/form/form.blade.php index 18088790..8d7c02d0 100644 --- a/resources/views/admin/pages/products/form/form.blade.php +++ b/resources/views/admin/pages/products/form/form.blade.php @@ -6,14 +6,15 @@ @section('content')