php - 使用 Lumen 通过 URL 传递限制和偏移值
问题描述
我是 Resfully 服务和 Lumen(Laravel 微框架)的新手。
我想将 /books?limit=10&offset=5 参数传递给控制器并将其设置在 json 响应中,但我不知道如何。
网页.php
$router->get('/books/', ['uses' => 'BooksController@index']);
BooksController.php
public function index()
{
$books = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
})
->offset(5) // This should have an default value until the user pass a value through url
->limit(30) // This should have an default value until the user pass a value through url
->orderBy('id', 'asc')
->get(['id', 'category', 'description']);
$status = !is_null($books) ? 200 : 204;
return response()->json($books, $status);
}
请你帮助我好吗?
谢谢,
解决方案
您可以使用Request对象来做到这一点:
use Illuminate\Http\Request;
public function index(Request $request)
{
$limit = $request->input('limit');
$offset = $request->input('offset');
$books = PartnersBooks::where('is_direct', '=', 1)
->with('direct')
->whereHas('direct', function ($query) {
$query->enable()
->select(['id', 'book_id', 'name', 'devices', 'flow', 'restrictions', 'countries', 'targeting']);
})
->offset($offset) // This should have an default value until the user pass a value through url
->limit($limit) // This should have an default value until the user pass a value through url
->orderBy('id', 'asc')
->get(['id', 'category', 'description']);
$status = !is_null($books) ? 200 : 204;
return response()->json($books, $status);
}
推荐阅读
- python - 从 Etsy 对项目变体进行排序
- c# - FromJson 字符串到列表
- angular - 如何在对象引用更改时制作类似于 :enter 和 :leave 的动画
- python - 如何使用日期时间级别的数据计算 n 天的滚动平均值?
- ios - 在 Swift 中使用 calloutAccessoryControlTapped 方法时,UIAlertController 视图未出现在地图上
- php - Laravel 的代理库。怎么了?
- javascript - 单个存根的不同结果
- flutter - 如何检查用户是否使用带有 null 安全 firebaseauth (Flutter) 的流构建器登录
- java - Spring 安全标签库
- excel - 使用 VBA 将图片从 Excel 粘贴到 Word 后尝试创建封闭书签