Help us understand the problem. What is going on with this article?

【Laravel】【PHP】json_decode(配列化)/ json_encode(JSON化)

More than 3 years have passed since last update.

EloquentでとってきたDBの全データを取得 & dd() でデバッグすると

MemberController.php
<?php

namespace App\Http\Controllers\;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use App\Http\Model\Member;
use Carbon\Carbon;

class MemberController extends Controller
{
    protected $request;
    public function __construct(
        Request $request
    ){
        $this->request = $request;
    }

    public function getMemberList() {
        $members = Member::all();
        dd($members);
    }
}

こういう感じでブラウザに返ってくる。

Collection {#281 ▼
  #items: array:8 [▼
    0 => Member {#282 ▼
      #table: "account"
      #fillable: array:1 [▶]
      #connection: null
      #primaryKey: "id"
      #perPage: 15
      +incrementing: true
      +timestamps: true
      #attributes: array:23 [▼
        "mail" => taro.yamada@gmail.com
        "accountcode__c" => "100000"
        "name" => "山田太郎"
        "id" => 1
        "createddate" => "2016-03-25 00:43:30"
      ]
      #original: array:23 [▶]
      #relations: []
      #hidden: []
      #visible: []
      #appends: []
      #guarded: array:1 [▶]
      #dates: []
      #dateFormat: null
      #casts: []
      #touches: []
      #observables: []
      #with: []
      #morphClass: null
      +exists: true
      +wasRecentlyCreated: false
    1 => Member {#283 ▶}
    2 => Member {#284 ▶}
  ]
}

ちなみにDBの値をそのままBladeに表示するとJSONで表示される

dd() を外してビュー(Blade)に埋め込むと

test.blade.php
@foreach($members as $member=>$index)
    <tr>
        <td>{{ $index }}</td>
    </tr>
@endforeach

ブラウザには JSON として表示される。

{"name": "山田太郎", "email": "taro.yamada@gmail.com"}{"name": "田中花子", "email": "hanako.tanaka@gmail.com"}{"name": "佐藤一郎", "email": "ichiro.sato@gmail.com"}

でもこれじゃ読みにくすぎるので

名前 email
山田太郎 taro.yamada@gmail.com
田中花子 hanako.tanaka@gmail.com
佐藤一郎 ichiro.sato@gmail.com

みたいにちゃんとテーブル表示したいなら

test.blade.php
@foreach($members as $member=>$index)
    <tr>
        <td>{{ $index->name }}</td>
        <td>{{ $index->email }}</td>
    </tr>
@endforeach

json_encode(JSON化)

値をJSON形式にしてくれる関数。

第二引数に JSON_PRETTY_PRINT をつけると読みやすい形にしてくれる。

test.php
$array = [];
$json = json_encode($array,JSON_PRETTY_PRINT);

json_decode(配列化)

JSONをPHPの配列にしてくれる。

true/falsearray/object で返ってくる。
ちなみにデフォルトだとfalseになり object で返ってくるので注意。

MemberController.php
public function getMemberList() {
    $members = Member::all();

    $json = json_decode($members, true);
    dd($json);
}

こんな感じでphp配列として返ってくる。

array:8 [▼
  0 => array:23 [▼
        "mail" => taro.yamada@gmail.com
        "accountcode__c" => "100000"
        "name" => "山田太郎"
        "id" => 1
        "createddate" => "2016-03-25 00:43:30"
  ]
  1 => array:23 [▶]
  2 => array:23 [▶]
]
Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした