带有查询参数的 Laravel REST API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14546752/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Laravel REST API with query parameters
提问by Sneaksta
So I have part of an API up and running, but am stuck on a few things. I am working with Laravel's REST controllers and am loving the ability to use methods such as Response::eloquent($query);
But, in using this:
所以我已经启动并运行了 API 的一部分,但我坚持了一些事情。我正在使用 Laravel 的 REST 控制器,并且喜欢使用诸如Response::eloquent($query);
But 之类的方法的能力,在使用它时:
- How should I handle query parameters (e.g id=27&order_by=timestamp), if the resource that I am getting is joining several tables?
- Should I get the values from the database, and then construct an array with only the certain fields that I want to display for API access? And how would this then work with query parameters?
- 如果我获得的资源加入了多个表,我应该如何处理查询参数(例如 id=27&order_by=timestamp)?
- 我是否应该从数据库中获取值,然后构造一个数组,其中只包含我想为 API 访问显示的某些字段?那么这将如何与查询参数一起使用?
回答by Mulkave
First off I would recommend that you watch Teach a Dog to REST[Link broken] for more information about formatting your REST routes.
首先,我建议您观看Teach a Dog to REST[链接已损坏] 以获取有关格式化 REST 路由的更多信息。
As for your questions:
至于你的问题:
- You can handle input fields through the
methods as mentioned in laravel input & cookies docs. You might also have to validate incoming data with laravel's Validator class.Input::get() and Input::all()
Say you acquired the id and order_by fields from the input as mentioned in your example:
$id = Input::get('id'); $order_criteria = Input::get('order_by');
- 您可以通过laravel input & cookies docs 中
提到的方法处理输入字段。您可能还需要使用 laravel 的 Validator 类来验证传入的数据。Input::get() and Input::all()
假设您从示例中提到的输入中获取了 id 和 order_by 字段:
$id = Input::get('id'); $order_criteria = Input::get('order_by');
using Fluent Query Builder
使用 Fluent Query Builder
DB::table('dbtable')->where('id', '=', $id)->order_by($order_criteria, 'desc')->first();
// Using first() since we are pretty sure that we are getting only one result out, for a different criteria where more than a result could be returned you might want to use ->get() instead.
// 使用 first() 因为我们很确定我们只得到一个结果,对于一个不同的标准,其中可以返回多个结果,你可能想要使用 ->get() 代替。
回答by selahattinunlu
I released a package for this.
我为此发布了一个包。
You can check this out https://github.com/selahattinunlu/laravel-api-query-builder
你可以看看这个https://github.com/selahattinunlu/laravel-api-query-builder
For wiki page: https://github.com/selahattinunlu/laravel-api-query-builder/wiki
对于维基页面:https: //github.com/selahattinunlu/laravel-api-query-builder/wiki
This package creates query with url parameters.
这个包创建带有 url 参数的查询。
Example:
例子:
/api/users?name=se*&age!=18&order_by=age,asc&limit=2&columns=name,age,city_id&includes=city
/api/users?name=se*&age!=18&order_by=age,asc&limit=2&columns=name,age,city_id&includes=city
And resulting query
结果查询
Users::with(['city'])->select(['name', 'age', 'city_id'])
->where('age', '!=', 18)
->where('name', 'like', 'se%')
->orderBy('age', 'asc')
->take(2)
Usage:
用法:
$queryBuilder = new QueryBuilder(new User, $request);
return response->json([
'data' => $queryBuilder->build()->paginate(),
.
.
]);
That's all.
就这样。