This commit is contained in:
Edward Yang 2022-08-15 17:53:26 +08:00
parent a91e403350
commit 8ca50a622d
3 changed files with 21 additions and 7 deletions

View File

@ -17,6 +17,8 @@ class Product extends Base
'images' => 'array', 'images' => 'array',
]; ];
protected $appends = ['image'];
public function categories() public function categories()
{ {
return $this->belongsToMany(Category::class, ProductCategory::class)->withTimestamps(); return $this->belongsToMany(Category::class, ProductCategory::class)->withTimestamps();
@ -66,4 +68,10 @@ class Product extends Base
{ {
return shop_route('products.show', ['product' => $this]); return shop_route('products.show', ['product' => $this]);
} }
public function getImageAttribute()
{
$images = $this->images ?? [];
return $images[0] ?? '';
}
} }

View File

@ -15,16 +15,22 @@ use Illuminate\Http\Resources\Json\JsonResource;
class WishlistDetail extends JsonResource class WishlistDetail extends JsonResource
{ {
/**
* @throws \Exception
*/
public function toArray($request): array public function toArray($request): array
{ {
$product = $this->product;
$masterSku = $product->master_sku;
$image = $this->product->image ?: $masterSku->image;
$data = [ $data = [
'id' => $this->id, 'id' => $this->id,
'product_id' => $this->priduct_id, 'product_id' => $this->product_id,
'image' => image_resize($this->product->image, 100, 100), 'image' => image_resize($image),
'product_name' => $this->product->description->name, 'product_name' => $product->description->name,
'price' => currency_format($this->product->price) 'price' => currency_format($product->price)
]; ];
return $data; return $data;
} }
} }

View File

@ -37,7 +37,7 @@
<td>{{ $item['price'] }}</td> <td>{{ $item['price'] }}</td>
<td class="text-end"> <td class="text-end">
<div class=""> <div class="">
<button class="btn btn-dark btn-sm add-cart">查看详情</button> <a class="btn btn-dark btn-sm add-cart" href="{{ shop_route('products.show', $item['product_id']) }}">查看详情</a>
<button class="btn btn-danger btn-sm remove-wishlist"><i class="bi bi-x-lg"></i></button> <button class="btn btn-danger btn-sm remove-wishlist"><i class="bi bi-x-lg"></i></button>
</div> </div>
</td> </td>
@ -72,4 +72,4 @@
}); });
}); });
</script> </script>
@endpush @endpush