I'm struggling to convert the Json value received from my controller into a properly readable JavaScript value.
This is my controller:
$room = Room::
select('id', 'name', 'capacity', 'status')
->get();
$this->rooms = json_encode($room);;
return view('admin.rooms.index', $this->data);
When I use {!!$room!!} in the view, I get:
[ {"id":1,"name":"room1","capacity":4,"status":"dirty"},{"id":2,"name":"room2","capacity":5,"status":"clean"},{"id":3,"name":"room3","capacity":5,"status":"clean"} ]
So, I have the Json value I need.
However, when I use {!!$room!!} in my script:
function loadResources() {
$.post( "{!!$rooms!!}",
{ capacity: $("#filter").val() },
function(data) {
dp.resources = data;
dp.update();
});
}
I encounter an error: Uncaught SyntaxError: missing ) after argument list.
That's the error message I receive.
If I create a file and put the Json value in it:
function loadResources() {
$.post( "room.json",
{ capacity: $("#filter").val() },
function(data) {
dp.resources = data;
dp.update();
});
}
Everything works fine in this case.
I tried using JSON.parse() to make the value JavaScript-readable, but it didn't work.
How can I effectively use that Json value in my JavaScript code?