Answer the question
In order to leave comments, you need to log in
Laravel pagination() and API response. Why is an array returned and not an object?
Tell me why when I write:
$books = BookModel::paginate($take, $offset);
$books = $books->each(function ($book) {
$book->setLink();
$book->setFileSize();
});
return response()->json(['response' => $books], 200);
Answer the question
In order to leave comments, you need to log in
The main mistake was overwriting $books. It was necessary like this:
$books = BookModel::paginate($take, $offset);
$books->each(function ($book) {
$book->setLink();
$book->setFileSize();
});
return response()->json(['response' => $books->toArray()], 200);
toJson method. Have you read the doc?
https://laravel.com/docs/5.4/pagination#converting...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question