Using put method on Laravel Eloquent Collection will always return an array

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Using put method on Laravel Eloquent Collection will always return an array



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:



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(IlluminateSupportCollection)#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?




2 Answers
2



Instead of using put method like below:


$information -> put('galleryImages', $galleryItems);



Try using concat method as below:


$information ->concat(['galleryImages' => $galleryItems ]);





This does not work unfortunately. It is similar to the behaviour of 'put()' method. I have also tried merge and push methods. In all cases I cannot update the initial Array '0' as shown in the PHP Object Dump above.
– yoyo
Aug 10 at 12:47



I managed to solve this issue by avoiding the response() -> json() method, instead I manually used json_encode on the Arrays.



Here is the code for those interested:


// To avoid Array getting returned, use first()
$finalCardInformation = $cardInformation -> first();

$imagesArray = json_encode($finalGalleryImages);

array_push($finalCardInformation, $imagesArray);

$jsonResponse = response($finalCardInformation, 200)
->header('Content-Type', 'application/json');



It's not the most elegant, but seems to work fine.






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

Creating a leaderboard in HTML/JS