I have a Response, that is of the following:
[{
"id":7,
"name": "Product Name",
"versions":[
{
"id":1,
"amount":199
},
{
"id":2,
"amount":99
}
]
}]
In Laravel Controller I did the following,
$q = Product::select(*)->with('getProductVersion');
$product = $q->WhereHas('getProductVersion', function($q){
$q->orderBy('amount', 'asc'); })->get();
return $product;
getProductVersion
is a ManytoMany relation between Product and Version.
However the Ordering isnt correct, the Expected result is
[{
"id":7,
"name": "Product Name",
"lowest_amount": 99,
"versions":[
{
"id":2,
"amount":99
},
{
"id":1,
"amount":199
}
]
}]
Sort the Array of Object and Print the lowest value to Parent Array.