简体   繁体   English

获取请求中的Laravel键值对

[英]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. 我正在使用Laravel 5.4和Axios向后端发出异步请求,并根据api端点的get请求参数检索数据。

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. 我以为您可以使用点表示法,但是无论如何您都可以尝试将其从json转换为关联数组。

$sort = json_decode($request->query('sort'), true);

This should then allow you to do something like, $sort['fieldName'] . 然后,这应该允许您执行$sort['fieldName'] The true parameter tells the decode to turn it into an associative array versus being returned an object. true参数告诉解码将其转换为关联数组,而不是返回对象。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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