diff --git a/beike/Models/Product.php b/beike/Models/Product.php index 8e7ad724..0fb04272 100644 --- a/beike/Models/Product.php +++ b/beike/Models/Product.php @@ -10,7 +10,7 @@ class Product extends Base use HasFactory; use SoftDeletes; - protected $fillable = ['image', 'video', 'position', 'active', 'variables']; + protected $fillable = ['images', 'video', 'position', 'active', 'variables']; protected $attributes = [ 'image' => '' ]; @@ -18,6 +18,7 @@ class Product extends Base 'active' => 'boolean', 'images' => 'array', 'variables' => 'array', + 'images' => 'array', ]; public function categories() diff --git a/beike/Models/ProductSku.php b/beike/Models/ProductSku.php index 35c86c52..25b24141 100644 --- a/beike/Models/ProductSku.php +++ b/beike/Models/ProductSku.php @@ -8,10 +8,11 @@ class ProductSku extends Base { use HasFactory; - protected $fillable = ['product_id', 'variants', 'position', 'image', 'model', 'sku', 'price', 'origin_price', 'cost_price', 'quantity', 'is_default']; + protected $fillable = ['product_id', 'variants', 'position', 'images', 'model', 'sku', 'price', 'origin_price', 'cost_price', 'quantity', 'is_default']; protected $casts = [ 'variants' => 'array', + 'images' => 'array', ]; public function product() diff --git a/beike/Repositories/CustomerRepo.php b/beike/Repositories/CustomerRepo.php index 39d77c99..2ecc35f9 100644 --- a/beike/Repositories/CustomerRepo.php +++ b/beike/Repositories/CustomerRepo.php @@ -141,5 +141,10 @@ class CustomerRepo return $customer->wishlists()->with('product.description')->paginate(20); } + + public static function inWishlist($product, $customer) + { + + } } diff --git a/beike/Shop/Http/Resources/ProductDetail.php b/beike/Shop/Http/Resources/ProductDetail.php index aefe47ff..4bc1c011 100644 --- a/beike/Shop/Http/Resources/ProductDetail.php +++ b/beike/Shop/Http/Resources/ProductDetail.php @@ -11,6 +11,7 @@ namespace Beike\Shop\Http\Resources; +use Beike\Repositories\CustomerRepo; use Illuminate\Http\Resources\Json\JsonResource; class ProductDetail extends JsonResource @@ -24,10 +25,13 @@ class ProductDetail extends JsonResource 'id' => $this->id, 'name' => $this->description->name ?? '', 'description' => $this->description->description ?? '', - 'image' => image_resize($this->image), + 'images' => array_map(function ($image) { + return image_resize($image); + }, $this->images ?? []), 'category_id' => $this->category_id ?? null, 'variables' => $this->decodeVariables($this->variables), 'skus' => SkuDetail::collection($this->skus)->jsonSerialize(), + 'in_wishlist' => CustomerRepo::inWishlist($this, current_customer()), ]; } diff --git a/beike/Shop/Http/Resources/ProductList.php b/beike/Shop/Http/Resources/ProductList.php index f0df0b03..f2a7bd79 100644 --- a/beike/Shop/Http/Resources/ProductList.php +++ b/beike/Shop/Http/Resources/ProductList.php @@ -31,7 +31,7 @@ class ProductList extends JsonResource 'url' => shop_route('products.show', ['product' => $this]), 'price' => $this->price, 'origin_price' => $this->origin_price, - 'image' => image_resize($this->image), + 'images' => image_resize($this->images[0] ?? ''), 'price_format' => currency_format($this->price), 'category_id' => $this->category_id ?? null, ]; diff --git a/beike/Shop/Http/Resources/SkuDetail.php b/beike/Shop/Http/Resources/SkuDetail.php index 26c72275..1e7d3b38 100644 --- a/beike/Shop/Http/Resources/SkuDetail.php +++ b/beike/Shop/Http/Resources/SkuDetail.php @@ -21,7 +21,9 @@ class SkuDetail extends JsonResource 'id' => $this->id, 'variants' => $this->variants, 'position' => $this->position, - 'image' => image_resize('catalog' . $this->image, 600, 600), + 'image' => array_map(function ($image) { + return image_resize($image, 600, 600); + }, $this->images ?? []), 'model' => $this->model, 'sku' => $this->sku, 'price' => $this->price, diff --git a/database/migrations/2022_07_26_073911_create_product_images.php b/database/migrations/2022_07_26_073911_create_product_images.php new file mode 100644 index 00000000..d8d9f332 --- /dev/null +++ b/database/migrations/2022_07_26_073911_create_product_images.php @@ -0,0 +1,42 @@ +dropColumn('image'); + $table->json('images')->nullable(); + }); + Schema::table('product_skus', function (Blueprint $table) { + $table->dropColumn('image'); + $table->json('images')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table('products', function (Blueprint $table) { + $table->dropColumn('images'); + $table->string('image')->default(''); + }); + Schema::table('product_skus', function (Blueprint $table) { + $table->dropColumn('images'); + $table->string('image')->default(''); + }); + } +}