When I send an empty object to my Laravel API, it converts it into an empty array. This is a problem because I want to store this empty object as it is in my database.
Here is the body I send :
{
"shouldBeEmptyArray": [],
"shouldBeEmptyObject": {},
}
and here is the response I get if I just return the request content in my controller :
[
[],
[]
]
If think this has something to do with the json()
method inside Request.php
, which use true
value for the associative parameter of json_decode()
, but I can’t find a way to make it work the way I want (and I don’t really want to change this file because it’s not a file of my project. I tried to override the method in my own Request custom class by extending Request but I doesn’t work). Here is the code of the json()
method of Request.php
:
/**
* Get the JSON payload for the request.
*
* @param string|null $key
* @param mixed $default
* @return SymfonyComponentHttpFoundationParameterBag|mixed
*/
public function json($key = null, $default = null)
{
if (! isset($this->json)) {
$this->json = new InputBag((array) json_decode($this->getContent(), true));
}
if (is_null($key)) {
return $this->json;
}
return data_get($this->json->all(), $key, $default);
}