I've got a problem regarding JSON.
The set-up is very simple. I'm using the Lumen framework and within the 'Controller' class I've got a function which gets some information from the database which I want to use in another controller called 'GameController'. Here's the code that matters:
class GameController extends Controller
{
...
public function index(Request $request)
{
$jsonGame = $this->getItem('games', 'gametitle', $request->input('title'));
}
...
}
class Controller extends BaseController
{
public function getItem($table, $column, $item)
{
try{
$result = app('db')->table($table)
->where($column, $item)
->take(1)
->get();
if(count($result) != 0) {
return Response::json($result);
}
return Response::notFound('Item not found');
}
catch(QueryException $e) {
...
}
}
}
Now the problem is that, as I try to decode the json in the variable $jsonGame and check if there are no errors, there's always the errorcode 4 and the decoding failed. I've checked it with the following:
if (json_last_error() === JSON_ERROR_NONE) {
echo 'json';
} else {
echo json_last_error(); // gives error code 4
}
So then I began checking the json manually, and this is what it outputs. The output of echo $jsonGame:
HTTP/1.0 200 OK Cache-Control: no-cache, private Content-Type: application/json Date: Mon, 24 Jul 2017 12:58:03 GMT
[{"gameid":2,"gametitle":"rocket league","gamedescription":"","created_at":"2017-07-24 07:02:43","updated_at":"2017-07-24 07:02:43"}]
The output of return $jsonGame:
[ { "gameid": 2, "gametitle": "rocket league", "gamedescription": "", "created_at": "2017-07-24 07:02:43", "updated_at": "2017-07-24 07:02:43" } ]
So as you can see the above seems to be valid JSON, and I'm melting my brain over this. Why isn't decoding working? Why does it give errorcode 4?