Vladimir Djukic
Vladimir Djukic

Reputation: 2072

Pass 2 params with Laravel REST api [Laravel 5]

I need to have 2 params inside my route:

/api/comments/2/?page=2

First for whole page second for items on that page(pagintaion)

I user REST api:

Route::resource('api/comments', 'CommentController');

and here is my controller, for show method I can just past one param, but I need 2:

public function show($id,  Comment $comm)

{

return $comm->apiGetComments($id);

}

And here is my model:

public function apiGetComments($id){
    $this->id = $id;
    if(ctype_digit($id)){
    $data = $this->recusative(0);
    $page = 1; // Get the current page or default to 1, this is what you miss!
    $perPage = 1;
    $offset = ($page * $perPage) - $perPage;
    return new LengthAwarePaginator(array_slice($data, $offset, $perPage, true), count($data), $perPage, $page, ['path' => Request::url(), 'query' => Request::query()]);
    }
}

When I do like this:

localhost/api/comments/1/?page=1

and then change page

localhost/api/comments/1/?page=2

nothing change ... I just have first link from page 1... Anyone can help me solve this?

Upvotes: 2

Views: 109

Answers (1)

Amo
Amo

Reputation: 2944

You're setting the page variable to 1 and not to the request variable that may be present - which you're then passing to the LengthAwarePaginator.

Try:

$page = Request::input('page') ?: 1;

Upvotes: 1

Related Questions