I am using Lumen (Laravel Eloquent ORM) to create a web service to use in conjunction with an app I am working on. I have a Joomla website, which I use Lumen to access resources I need from the db. I'm using Lumen to get these resources into a collection, then return a new JSON object.
To give an overview of the problem, I am creating two Collections:
- Object properties - (Collection 0)
- Associated images to the object (there will be at least 2 images, sometimes more) - (Collection 1)
I want Laravel to return a JSON object for Collection 0 (above), which will include a JSON Array containing the data of Collection 1.
Here is my code:
<?php
$current = Card::find($id);
//Collection 0
$information = $current -> cardData -> map(function($col) {
return collect($col -> toArray())
->only(['id', 'ccard_cardno', 'ccard_year', 'ccard_catc', 'ccard_catt', 'ccard_issued', 'ccard_valc',
'ccard_valtod', 'ccard_valfac', 'ccard_man', 'ccard_info'])
->all();
});
//Collection 1
$galleryItems = $current -> galItems -> pluck('ccard');
After processing the data as I need, I now add the galleryItems property to my Collection 0:
$galleryItems= json_encode($finalGalleryImages);
$information -> put('galleryImages', $galleryItems);
This does work fine, however each collection will be represented as a different object which is not what I want.
What I want is the below:
{
"id": 528,
"ccard_cardno": "...",
"ccard_year": "...",
"ccard_catc": "...",
"ccard_catt": "...",
"ccard_issued": "...",
"ccard_valc": "...",
"ccard_valtod": "...",
"ccard_valfac": "...",
"ccard_man": "...",
"ccard_info": "...",
"galleryImages": "[{\"thumb\":\"...",\"image\":\"...\",\"title\":\"...\"},...]"
}
(The galleryImages property contains array of photos associated with my object. )
What I actually get is Laravel will create a new object for collection "0":
{
"0": {
"id": 528,
"ccard_cardno": "...",
"ccard_year": "...",
"ccard_catc": "...",
"ccard_catt": "...",
"ccard_issued": "...",
"ccard_valc": "...",
"ccard_valtod": "...",
"ccard_valfac": "...",
"ccard_man": "...",
"ccard_info": "...",
},
"galleryImages": "[{\"thumb\":\"...",\"image\":\"...\",\"title\":\"...\"},...]"
}
The solution to this problem is to call the first() method on $information. In this case the object I put into the collection is excluded! I will get just (galleryImages property is missng)):
{
"id": 528,
"ccard_cardno": "...",
"ccard_year": "...",
"ccard_catc": "...",
"ccard_catt": "...",
"ccard_issued": "...",
"ccard_valc": "...",
"ccard_valtod": "...",
"ccard_valfac": "...",
"ccard_man": "...",
"ccard_info": "..."
}
Here is the object after the information is put() into my collection:
object(Illuminate\Support\Collection)#56 (1) {
["items":protected]=>
array(2) {
[0]=>
array(11) {
["id"]=>
int(529)
["ccard_cardno"]=>
string(4) "..."
["ccard_year"]=>
string(4) "..."
["ccard_catc"]=>
string(2) "..."
["ccard_catt"]=>
string(10) "..."
["ccard_issued"]=>
string(8) "..."
["ccard_valc"]=>
string(9) "..."
["ccard_valtod"]=>
string(7) "..."
["ccard_valfac"]=>
string(6) "..."
["ccard_man"]=>
string(22) "..."
["ccard_info"]=>
string(337) "..."
}
["galleryImages"]=>
string(262) "[{"thumb":"...","image":"...","title":".."},...]"
}
}
So it is clear why $response -> json() will now return an Array. My question is, how to I add the values to my Collection 0 using Eloquent?