diff --git a/beike/Models/Product.php b/beike/Models/Product.php index 663a7420..a0bd4ec2 100644 --- a/beike/Models/Product.php +++ b/beike/Models/Product.php @@ -17,6 +17,8 @@ class Product extends Base 'images' => 'array', ]; + protected $appends = ['image']; + public function categories() { return $this->belongsToMany(Category::class, ProductCategory::class)->withTimestamps(); @@ -66,4 +68,10 @@ class Product extends Base { return shop_route('products.show', ['product' => $this]); } + + public function getImageAttribute() + { + $images = $this->images ?? []; + return $images[0] ?? ''; + } } diff --git a/beike/Shop/Http/Resources/Account/WishlistDetail.php b/beike/Shop/Http/Resources/Account/WishlistDetail.php index f997d0ca..81818217 100644 --- a/beike/Shop/Http/Resources/Account/WishlistDetail.php +++ b/beike/Shop/Http/Resources/Account/WishlistDetail.php @@ -15,16 +15,22 @@ use Illuminate\Http\Resources\Json\JsonResource; class WishlistDetail extends JsonResource { + /** + * @throws \Exception + */ public function toArray($request): array { + $product = $this->product; + $masterSku = $product->master_sku; + $image = $this->product->image ?: $masterSku->image; + $data = [ 'id' => $this->id, - 'product_id' => $this->priduct_id, - 'image' => image_resize($this->product->image, 100, 100), - 'product_name' => $this->product->description->name, - 'price' => currency_format($this->product->price) + 'product_id' => $this->product_id, + 'image' => image_resize($image), + 'product_name' => $product->description->name, + 'price' => currency_format($product->price) ]; - return $data; } } diff --git a/themes/default/account/wishlist.blade.php b/themes/default/account/wishlist.blade.php index f758b1e9..533e47d4 100644 --- a/themes/default/account/wishlist.blade.php +++ b/themes/default/account/wishlist.blade.php @@ -37,7 +37,7 @@