LoginSignup
4
4

More than 5 years have passed since last update.

Laravel で HTTP Status 201 な JSON を返したい

Posted at

タイトル通り、JSON を返すけど、HTTP Status が 200 以外のステータスコードを返したい時のやりかた。

<?php namespace App\Http\Controllers;

use Illuminate\Http\JsonResponse;

class TestController extends Controller{
  public function postIndex ( CreateRequest $request ) {
    // ごにょごにょ操作

    $obj = \App\Model\Test::findOrCreate( $request->all() );

    return new JsonResponse(
        [
            'success' => "OK",
            "data" => $obj->toJSON()
        ],
        201 );
  }
}
4
4
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
4
4