Php 使用流明通过URL传递限制和偏移值

Php 使用流明通过URL传递限制和偏移值,php,laravel,eloquent,lumen,Php,Laravel,Eloquent,Lumen,我对Resfully服务和Lumen(Laravel micro framework)是新手 我想将/books?limit=10&offset=5参数传递给控制器,并在json响应上设置它,但我不知道如何设置 web.php $router->get('/books/', ['uses' => 'BooksController@index']); public function index() { $books = PartnersBooks::where('is_dir

我对Resfully服务和Lumen(Laravel micro framework)是新手

我想将/books?limit=10&offset=5参数传递给控制器,并在json响应上设置它,但我不知道如何设置

web.php

$router->get('/books/', ['uses' => 'BooksController@index']);
 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);
}
BooksController.php

$router->get('/books/', ['uses' => 'BooksController@index']);
 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);
}
你能帮帮我吗

谢谢,

您可以使用该对象执行以下操作:

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);
}

$request->input()
也接受第二个参数作为默认值。因此,您可以这样设置默认值:
$offset=$request->input('offset',5)
。和极限类似。完成!非常感谢。