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

时间:2018-04-24 19:35:04

标签: php laravel eloquent lumen

我是Resfully服务和Lumen(Laravel微框架)的新手。

我想将/ books?limit = 10& offset = 5参数传递给控制器​​并将其设置为json响应,我无法弄清楚如何。

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

你能帮我吗?

谢谢,

1 个答案:

答案 0 :(得分:0)

您可以使用Request对象执行此操作:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
  <link rel="stylesheet" href="index.css">
</head>
<body>

<div class="wrapper">

<div class="discovery-list">Discovery here</div>

<div class="drag-handle">
  <div class="text-area-1">X</div>
  <div class="text-area-2">xx</div>
  <div class="text-area-3">Title here</div>
  <div class="text-area-4">Share</div>  
</div>

<div class="sharing-list">sharing list</div>

</div>

<script src="index.jsx">
</script>
</body>
</html>