Передать значения пределов и смещений через URL с помощью Lumen - PullRequest
0 голосов
/ 24 апреля 2018

Я новичок в сервисах 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 голосов
/ 24 апреля 2018

Вы можете использовать объект Запрос , чтобы сделать это:

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