Illuminate\Http\JsonResponseインスタンスからjsonを取り出す

レスポンスをjsonで返すapiを別のアクションで呼んでその結果に対して配列処理をかけたい場合。

参考

laravel.com

そのapiが下記のようにレスポンスを返す場合はIlluminate\Http\JsonResponseインスタンスが結果として返されている。

return response()->json( $result_data );

これはjsonではないのでjson_decodeをかけるとnullが帰る(json_decodeはjsonとして正しい形式でないデータを引数に渡すとnullを返す)。Illuminate\Http\JsonResponseインスタンスからjson形式のデータのみ抽出したい場合はIlluminate\Http\JsonResponseが持つcontent()メソッドを利用する。

$result       = $this->getUsers( $request, $some_attr );
$result_json  = $result->content()
$result_array = json_decode( $result_json, true );

上記のようにすれば結果を配列として処理することができる。