获取请求中的Laravel键值对

时间:2018-05-15 03:06:34

标签: php laravel vue.js request axios

我正在使用Laravel 5.4和Axios向我的后端发出异步请求,并根据我的api端点的get请求参数检索数据。

我可以在检查员看到我向服务器发出以下请求:

https://website.com/api/users?page=1&sort=%7B%22fieldName%22:%22lname%22,%22order%22:%22asc%22%7D&filter=

其解码为:

https://website.com/api/users?page=1&sort={"fieldName":"lname","order":"asc"}&filter=

看起来我可以通过以下方式成功获取查询:

return $request->query('sort');

返回:

data:
    fieldName: "lname"
    order: "asc"

但是当我使用时:

return $request->query('sort.fieldName');

我什么都没得到。我应该不使用dot notation吗?如何获取排序输入的每个键/值对?谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

我认为你可以使用点表示法,但无论如何你可以尝试将它从$sort = json_decode($request->query('sort'), true); 转换为关联数组。

$sort[‘fieldName’]

这应该允许你做true之类的事情。 $query = DB::table('order'); $fromorderdate = $request->get('fromorderdate') or ""; $toorderdate = $request->get('toorderdate') or ""; $fromdeliverydate = $request->get('fromdeliverydate') or ""; $todeliverydate = $request->get('todeliverydate') or ""; $orderstatus = $request->get('orderstatus') or ""; $ordertype = $request->get('ordertype') or ""; if (Input::get('weightrange')) { $weightrangearray = explode('.', Input::get('weightrange')); $weightrange1 = $weightrangearray[0]; $weightrange2 = $weightrangearray[1]; } if ($fromorderdate != "" && $toorderdate != "") { $query->whereBetween('orderdate', [$fromorderdate, $toorderdate]); $makequery = true; } if ($fromdeliverydate && $todeliverydate) { $query->whereBetween('orderdate', [$fromdeliverydate, $todeliverydate]); $makequery = true; } if ($fromdeliverydate && $todeliverydate) { $query->whereBetween('orderdate', [$weightrange1, $weightrange2]); $makequery = true; } if ($orderstatus) { $query->where('orderstatus', $orderstatus); $makequery = true; } if ($ordertype) { $query->where('ordertype', $ordertype); $makequery = true; } echo $query->toSql(); if ($makequery) { $order = $query->get(); } 参数告诉解码器将其转换为关联数组而不是返回对象。