Files
portal/app/Http/Resources/PackageResource.php
T
2022-02-23 13:42:12 +08:00

42 lines
1.6 KiB
PHP

<?php
namespace App\Http\Resources;
use App\Models\Order;
use App\Models\PackingList;
use Illuminate\Http\Resources\Json\JsonResource;
class PackageResource extends JsonResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
* @return array
*/
public function toArray($request)
{
$originalPackingList = $this->packingList->owner instanceof PackingList ? $this->packingList->owner->owner : $this->packingList->owner;
return [
'id' => $this->id,
'type' => $this->type,
'description' => $this->description,
'width' => floatval($this->width),
'height' => floatval($this->height),
'length' => floatval($this->length),
'weight' => $this->weight,
'quantity' => $this->quantity,
'cbm' => (($this->width / 100) * ($this->height / 100) * ($this->length / 100)) * $this->quantity,
'status' => $this->status,
$this->mergeWhen($originalPackingList->owner instanceof Order, [
'order' => New OrderResource($originalPackingList->owner)
]),
'order' => New OrderResource($this->packingList->owner instanceof Order ? $this->packingList->owner : $this->packingList->owner->owner),
'container' => new ContainerResource($this->packingList->owner instanceof Order ? $this->container : $this->packingList->owner->containers()->first()),
'transport' => new TransportResource($this->packingList->owner instanceof Order ? $this->transport : $this->packingList->owner->transports()->first())
];
}
}