✅ 戻り値の型は Illuminate\Http\JsonResponse
phpreturn response()->json(['message' => 'Hello, world!']);
このときの戻り値の型は:
phpIlluminate\Http\JsonResponse
🔍 JsonResponse とは?
JsonResponse クラスは、Laravel で使用されている Symfony の Symfony\Component\HttpFoundation\JsonResponse を拡張したものです。
つまり、
HTTP レスポンスオブジェクトであり、JSONデータをクライアントに返すためのものです。
💡 型ヒントの例
もしメソッドや関数で response()->json() を返すなら、次のように型指定できます:
phpuse Illuminate\Http\JsonResponse;
function getApiResponse(): JsonResponse {
return response()->json(['status' => 'OK']);
}
👀 注意点
JsonResponseは JSON 文字列そのものではなく、「HTTPレスポンスオブジェクト」です。- ブラウザや API クライアントがリクエストを送ると、
ヘッダー・ステータスコード・JSON本文を含めて返ってきます。
🔁 中のJSON文字列が欲しい場合は?
response()->json() を使うと、
そのままJSON文字列が得られるわけではなく、レスポンスとして返すためのオブジェクト
になります。
テストやログ出力のために中身が欲しいときは、こんな風に使えます:
php$response = response()->json(['foo' => 'bar']);
$json = $response->getContent(); // JSON文字列として取得
🎯 まとめ
| 使い方 | 型 |
|---|---|
response()->json([...]) | Illuminate\Http\JsonResponse |
$response->getContent() | string(JSON文字列) |


