I want to send a stringified JSON to one of the fields of an API request like following:
Decoded:
JavaScript
x
https://api.some.com/foo/bar?a=788&b=My Name&c=[{"name":"pejman"},{"Some":"thing"}]
I want to get c
parameter using $request->c
but I want to get it as a decoded JSON automatically in my controller.
This is my PHP code
MyRequest.php:
JavaScript
<?php
use InfyOmGeneratorRequestAPIRequest;
class MyRequest extends APIRequest
{
public function authorize()
{
return true;
}
public function rules()
{
return [
'a' => 'required',
'b' => 'required',
'c' => 'requried',
];
}
}
SomeController.php:
JavaScript
<?php
class SomeController extends Controller
{
public function store(MyRequest $request)
{
$c = $request->c;
$c = $request->json('c');
$c = $request->json()->all();
}
}
???? I want
$c
to be a JSON decoded automatically in my controller, How can I do that? Is that event possible to do this usingMyReqest
and how?
Advertisement
Answer
You can manipulate the request data using prepareForValidation()
method that located in IlluminateValidationValidatesWhenResolvedTrait
. So, implement this method in your MyRequest
class:
JavaScript
protected function prepareForValidation()
{
$this->merge([
'c' => json_decode($this->c),
]);
}