简体   繁体   中英

Laravel Key Value Pair in Get Request

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!

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM