29
25

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 5 years have passed since last update.

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

Posted at

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 [▶]
]
29
25
1

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
29
25

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?