$this->id, 'claimant_id' => $this->claimant_id, 'reference' => $this->reference, 'status' => $this->status, 'transport' => new TransportResource($this->transports()->first()), 'type' => $this->type, 'container' => new ContainerResource($this->containers->first()), $this->mergeWhen($this->owner instanceof Order, [ 'order' => New OrderResource($this->owner) ]), ]; //option 1: include_packages $data['packages'] = PackageBaseResource::collection([]); //option 4: include_suspended_invoice $data['suspended_invoice'] = new TransactionResource($this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereIn('status', [ApprovalStatus::SUSPENDED])->first()); //option 5: include_shipping_transaction WITH warehouse storage $shippingTransaction = $this->transactions()->where('type', TransactionType::SHIPPING_INVOICE)->whereNotIn('status', [ApprovalStatus::SUSPENDED, ApprovalStatus::EXPIRED])->first(); $transactions = $this->transactions()->whereNotIn('transactions.status', [0, 1])->whereIn('transactions.type', [TransactionType::SHIPPING_INVOICE, TransactionType::STORAGE_INVOICE])->get(); $storages = []; if($this->storages){ $transactionId = $shippingTransaction->id; $filteredStorages = array_filter($this->storages, function ($storage) use ($transactionId) { return isset($storage['parentInvoiceId']) && $storage['parentInvoiceId'] == $transactionId; }); $storages = $filteredStorages; } $data['shipping_transaction'] = count($storages) == 0 ? new TransactionWithStorageResource($shippingTransaction): null; $data['storages'] = $storages; $data['invoices'] = TransactionWithStorageResource::collection($transactions); return $data; } }