Skip to content

Commit 2523f49

Browse files
committed
Update OrderTransformer: add products include
1 parent 0ca9c3b commit 2523f49

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

controllers/Orders.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class Orders extends ApiController
1111

1212
public function index()
1313
{
14-
return $this->respondwithCollection($this->user->orders, new OrderTransformer);
14+
return $this->respondwithCollection($this->user->orders()->orderBy('created_at', 'desc')->get(), new OrderTransformer);
1515

1616
}
1717

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php namespace Octommerce\API\Transformers;
2+
3+
use Octobro\API\Classes\Transformer;
4+
use Octommerce\Octommerce\Models\Product;
5+
6+
class OrderProductTransformer extends Transformer
7+
{
8+
public function data(Product $product)
9+
{
10+
return [
11+
'id' => (int) $product->id,
12+
'sku' => $product->sku,
13+
'name' => $product->pivot->name,
14+
'qty' => (int) $product->pivot->qty,
15+
'price' => (float) $product->pivot->price,
16+
'discount' => (float) $product->pivot->discount,
17+
'images' => $this->images($product->images),
18+
];
19+
}
20+
21+
}

transformers/OrderTransformer.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class OrderTransformer extends Transformer
1111

1212
public $availableIncludes = [
1313
'invoice',
14+
'products',
1415
];
1516

1617
public function data(Order $order)
@@ -29,6 +30,11 @@ public function data(Order $order)
2930
'misc_fee' => (float) $order->misc_fee,
3031
'total' => (float) $order->total,
3132
'status_code' => $order->status_code,
33+
'status' => [
34+
'name' => $order->status->name,
35+
'color' => $order->status->color,
36+
'description' => $order->status->description,
37+
],
3238
'status_updated_at' => date($order->status_updated_at),
3339
'created_at' => date($order->created_at),
3440
];
@@ -39,4 +45,9 @@ public function includeInvoice(Order $order)
3945
return $this->item($order->invoice, new InvoiceTransformer);
4046
}
4147

48+
public function includeProducts(Order $order)
49+
{
50+
return $this->collection($order->products, new OrderProductTransformer);
51+
}
52+
4253
}

0 commit comments

Comments
 (0)