Having a struggle with validating JSON input for an api on Laravel 5.6. I've been trying the solution at How to get and validate application/json data in Laravel? but still doesn't resolve.
The supporting class:
<?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();
}
and the Controller:
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->json()->all()); die();
produces:
Array
(
[code] => 45678
[description] => My great item
)
so data is getting to server
*/
$validator = Validator::make($request->json()->all(), [
'code', 'required',
'description', 'required',
]);
if($validator->fails()){
$response->status = $response::status_failed;
$response->code = $response::code_failed;
foreach ($validator->errors()->getMessages() as $item) {
array_push($response->messages, $item);
}
} else {
$myItem = new MyItem();
$myItem->code = $request->code;
$myItem->description = $request->description;
$response->status = $response::status_ok;
$response->code = $response::code_ok;
$response->result = $myItem;
}
return FacadeResponse::json($response);
/*
Returns:
{
"status": "FAILED",
"code": 400,
"messages": [
[
"The 1 field is required."
],
[
"The 3 field is required."
]
],
"result": []
}
*/
}
}
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?
Any guidance would be greatly appreciated.