This commit is contained in:
Edward Yang 2022-08-15 18:42:45 +08:00
parent 51a7638b47
commit ceb0aa0686
3 changed files with 12 additions and 11 deletions

View File

@ -2,6 +2,7 @@
namespace Beike\Admin\Http\Resources;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
class ProductResource extends JsonResource
@ -9,22 +10,24 @@ class ProductResource extends JsonResource
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
* @param Request $request
* @return array
*/
public function toArray($request)
public function toArray($request): array
{
$masterSku = $this->master_sku;
$data = [
'id' => $this->id,
'images' => array_map(function ($image) {
return thumbnail($image);
}, $this->images ?? []),
'name' => $this->description->name ?? '',
'price_formatted' => $this->price_formatted,
'price_formatted' => currency_format($masterSku->price),
'active' => $this->active,
'position' => $this->position,
'created_at' => (string)$this->created_at,
'deleted_at' => (string)$this->deleted_at,
'created_at' => time_format($this->created_at),
'deleted_at' => time_format($this->deleted_at),
'url_edit' => admin_route('products.edit', $this->id),
];

View File

@ -59,11 +59,6 @@ class Product extends Base
return $this->hasOne(CustomerWishlist::class)->where('customer_id', current_customer() ? current_customer()->id : 0);
}
public function getPriceFormattedAttribute(): string
{
return '$' . $this->price;
}
public function getUrlAttribute()
{
return shop_route('products.show', ['product' => $this]);

View File

@ -120,6 +120,9 @@ class ProductRepo
$builder->onlyTrashed();
}
$sort = $data['sort'] ?? 'updated_at';
$order = $data['order'] ?? 'desc';
$builder->orderBy($sort, $order);
return $builder;
}