Reputation: 1241
I'm using Laravel 5.4 and Axios to make async requests to my backend and retrieve data based on the get request parameters of my api endpoint.
I can see in inspector I'm making the following request to the server:
https://website.com/api/users?page=1&sort=%7B%22fieldName%22:%22lname%22,%22order%22:%22asc%22%7D&filter=
Which decodes to:
https://website.com/api/users?page=1&sort={"fieldName":"lname","order":"asc"}&filter=
Looks like I can successfully get pieces of the query via:
return $request->query('sort');
which returns:
data:
fieldName: "lname"
order: "asc"
But when I use:
return $request->query('sort.fieldName');
I don't get anything. Should I not be using the dot notation? How do I get each key / value pair of the sort input? Thanks for any help!
Upvotes: 0
Views: 2012
Reputation: 1039
I thought you could use dot notation, but at any rate you could try just converting it from json
to an associative array.
$sort = json_decode($request->query('sort'), true);
This should then allow you to do something like, $sort[‘fieldName’]
. The true
parameter tells the decode to turn it into an associative array versus being returned an object.
Upvotes: 1