title: Validation of JSON Post using Laravel 5.6 api
body: <p>Having a struggle with validating JSON input for an api on Laravel 5.6.  I've been trying the
  solution at <a href="https://stackoverflow.com/questions/45255156/how-to-get-and-validate-application-json-data-in-laravel">How to get and validate application/json data in Laravel?</a>
  but still doesn't resolve.</p>
  
  <p>The supporting class:</p>
  
  <pre><code>&lt;?php
  namespace App\Http\Controllers\API;
  
  class ResponseObject
  {
      const status_ok = "OK";
      const status_fail = "FAIL";
      const code_ok = 200;
      const code_failed = 400;
      const code_unauthorized = 403;
      const code_not_found = 404;
      const code_error = 500;
  
      public $status;
      public $code;
      public $messages = array();
      public $result = array();
  }
  </code></pre>
  
  <p>and the Controller:</p>
  
  <p>
  
  <pre><code>namespace App\Http\Controllers\API;
  
  use App\Http\Resources\MyItemsResource;
  use App\MyItem;
  use Illuminate\Http\Request;
  use App\Http\Controllers\Controller;
  use \Illuminate\Http\Response;
  use \Illuminate\Support\Facades\Response as FacadeResponse;
  use \Illuminate\Support\Facades\Validator;
  use App\Http\Controllers\API\ResponseObject;
  
  
  
  class MyItemsController extends Controller
  {
      /**
       * Store a newly created resource in storage.
       *
       * @param  \Illuminate\Http\Request  $request
       * @return \Illuminate\Http\JsonResponse
       */
      public function store(Request $request)
      {
          $response = new ResponseObject;
  
          /*
          sending a body of
          {"code": "45678", "description": "My great item"}
  
          then:
          // print_r($request-&gt;json()-&gt;all()); die();
  
          produces:
              Array
              (
                  [code] =&gt; 45678
                  [description] =&gt; My great item
              )
           so data is getting to server
          */
  
          $validator = Validator::make($request-&gt;json()-&gt;all(), [
              'code', 'required',
              'description', 'required',
          ]);
  
          if($validator-&gt;fails()){
              $response-&gt;status = $response::status_failed;
              $response-&gt;code = $response::code_failed;
              foreach ($validator-&gt;errors()-&gt;getMessages() as $item) {
                  array_push($response-&gt;messages, $item);
              }
          } else {
              $myItem = new MyItem();
              $myItem-&gt;code = $request-&gt;code;
              $myItem-&gt;description = $request-&gt;description;
              $response-&gt;status = $response::status_ok;
              $response-&gt;code = $response::code_ok;
              $response-&gt;result = $myItem;
          }
  
          return FacadeResponse::json($response);
          /*
           Returns:
          {
              "status": "FAILED",
              "code": 400,
              "messages": [
                  [
                      "The 1 field is required."
                  ],
                  [
                      "The 3 field is required."
                  ]
              ],
              "result": []
           }
           */
      }
  }
  </code></pre>
  
  <p>Why does it refer to the fields as 1 field and 3 field?
  What am I missing that it doesn't pick up the fields as they are getting to the controller in the $request?</p>
  
  <p>Any guidance would be greatly appreciated.</p>