LoginSignup
0
1

laravelのcollectionで連想配列を追加する方法

Posted at

環境

  • php:8.1
  • laravel:9.23.0

対象

  • laravelのcollectionで連想配列を使いたい人

方法

以下のようにすると出来ます。

Controller.php
$users = DB::table('users')->get();
$items = collect();
foreach ($users as $user) {
    $itemLists = [];
    $itemLists['id'] = $user['id'];
    $itemLists['name'] = $user['name'];
    $itemLists['mail '] = $user['mail'];
    $items->push($itemLists);
}
0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1