如何从laravel Illuminate中的所有集合索引中获取价值?

时间:2016-06-09 06:56:49

标签: php laravel-5 illuminate-container

我必须在数据库中查询数据,然后在laravel中使用Illuminate,然后我想从该数组的所有索引中获取所有属性值。

array:36 [▼
  0 => RepaymentSchedule {#553 ▶}
  1 => RepaymentSchedule {#554 ▶}
  2 => RepaymentSchedule {#555 ▶}
  3 => RepaymentSchedule {#556 ▶}
  4 => RepaymentSchedule {#557 ▶}
  5 => RepaymentSchedule {#558 ▶}
  6 => RepaymentSchedule {#559 ▶}
  7 => RepaymentSchedule {#560 ▶}
  8 => RepaymentSchedule {#561 ▶}
  9 => RepaymentSchedule {#562 ▶}
  10 => RepaymentSchedule {#563 ▶}
  11 => RepaymentSchedule {#564 ▶}
  12 => RepaymentSchedule {#565 ▶}
  13 => RepaymentSchedule {#566 ▶}
  14 => RepaymentSchedule {#567 ▶}
  15 => RepaymentSchedule {#568 ▶}
  16 => RepaymentSchedule {#569 ▶}
  17 => RepaymentSchedule {#570 ▶}
  18 => RepaymentSchedule {#571 ▶}
  19 => RepaymentSchedule {#572 ▶}
  20 => RepaymentSchedule {#573 ▶}
  21 => RepaymentSchedule {#574 ▶}
  22 => RepaymentSchedule {#575 ▶}
  23 => RepaymentSchedule {#576 ▶}
  24 => RepaymentSchedule {#577 ▶}
  25 => RepaymentSchedule {#578 ▶}
  26 => RepaymentSchedule {#579 ▶}
  27 => RepaymentSchedule {#580 ▶}
  28 => RepaymentSchedule {#581 ▶}
  29 => RepaymentSchedule {#582 ▶}
  30 => RepaymentSchedule {#583 ▶}
  31 => RepaymentSchedule {#584 ▶}
  32 => RepaymentSchedule {#585 ▶}
  33 => RepaymentSchedule {#586 ▶}
  34 => RepaymentSchedule {#587 ▶}
  35 => RepaymentSchedule {#588 ▶}
]

我试着用下面的文件

$collapsed = $loan->schedule->all();
  dd($collapsed);

  $collection = $collapsed->each(function ($item, $key) {
    dd($key);
  });

1 个答案:

答案 0 :(得分:1)

在laravel 5.1及更高版本中,您可以使用Collection对象的pluck方法:

$plucked = $collapsed->pluck('id');

dd($plucked->all());

文档:https://laravel.com/docs/5.2/collections#method-pluck

但由于您使用的是L5.0,这是不可能的,我建议您通过PHP5方法array_column函数来解决这个问题:

$ids = array_column($collapsed->toArray(), 'id');
dd($ids);

编辑: 如果要检索多个属性,可以使用->lists()方法:

$values = $loan->schedule->lists('id', 'interest', 'principal');
dd($values);