如何:将Laravel集合折叠成逗号分隔的字符串?

时间:2019-11-27 04:26:18

标签: php laravel

如何:将Laravel集合折叠成逗号分隔的字符串?

dd($ids);

Illuminate\Support\Collection {#538 ▼
  #items: array:463 [▼
    0 => 30578786410269
    1 => 30578326028621
    2 => 30788548304669
    3 => 30788548607437
    4 => 30788592370221
    5 => 30788592672989
    6 => 30788592975757
    7 => 30492511411997
      ]
}

我如何将其转换为类似的字符串?:

30578786410269,30578326028621,30788548304669,30788548607437,30788592370221,30788592672989,30788592975757,30492511411997

2 个答案:

答案 0 :(得分:2)

使用implode提供的Illuminate\Support\Collection方法:

$imploded = $collection->implode(',');

Laravel 6.x Docs - Collections - Available Methods - implode

答案 1 :(得分:0)

您可以使用implode()

$data = $collection->implode(',');   // returns string

,您可以将collection转换为array

$data = $collection->toArray();     // returns array