流明查询字符串过滤器

时间:2019-09-26 11:13:38

标签: laravel lumen

我有一个REST API,可用于对给定集合中的多个字段进行排序。有什么方法可以将它们转换为URL的查询字符串吗?例如:

interface IFetcher {
  url: string;
  children(data: Array<{}>, error: string, isLoading: boolean): ReactElement;
}

const Fetcher: React.FC<IFetcher> = props => {
  const [data, setData] = useState<Array<{}>>([]);
  const [isLoading, setLoading] = useState<boolean>(false);
  const [error, setError] = useState<string>("");

  useEffect(() => {...});

  return props.children(data, error, isLoading);
};

这将按createdAt排序用户,然后命名。

Lumen / Laravel是否有任何默认方式将其转换为查询字符串参数以进行过滤。

1 个答案:

答案 0 :(得分:0)

您可以使用数组作为查询参数。

类似的东西:

https://example.com/users?sortby[createdAt]=asc&sortyby[name]=desc

将为您提供此对象:

{
    "sortby": {
        "createdAt": "asc",
        "name": "desc"
    }
}

因此,现在您可以轻松地遍历此对象并构建例如查询构建器。