dtrgqjcd877528 2018-08-10 10:54
浏览 239

在Laravel Eloquent Collection上使用put方法将始终返回一个数组

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?

  • 写回答

2条回答 默认 最新

  • dpevsxjn809817 2018-08-10 11:26
    关注

    Instead of using put method like below:

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

    Try using concat method as below:

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

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思