2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

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);
}
2
2
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
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?