This is mine
$art = Article::where(\'id\',$article)->firstOrFail();
$products = $art->products;
I just wanna take a limit \'p
Laravel 8 (This is worked for the version 7.16)
$art->products->skip($offset*$limit)->take($limit)->all();
https://laravel.com/docs/8.x/collections#method-take
Please try like this,
return Article::where('id',$article)->with(['products'=>function($products, $offset, $limit) {
return $products->offset($offset*$limit)->limit($limit);
}])
laravel have own function skip
for offset
and take
for limit
. just like below example of laravel query :-
Article::where([['user_id','=',auth()->user()->id]])
->where([['title','LIKE',"%".$text_val."%"]])
->orderBy('id','DESC')
->skip(0)
->take(2)
->get();
Try this sample code:
$art = Article::where('id',$article)->firstOrFail();
$products = $art->products->take($limit);
Laravel has a fast pagination method, paginate, which only needs to pass in the number of data displayed per page.
//use the paginate
Book::orderBy('updated_at', 'desc')->paginate(8);
you can use these method: offset
,limit
,skip
,take
offset,limit : where does the offset setting start, limiting the amount of data to be queried
skip,take: skip skips a few pieces of data and takes a lot of data
for example:
Model::offset(0)->limit(10)->get();
Model::skip(3)->take(3)->get();
//i use it in my project, work fine ~
class BookController extends Controller
{
public function getList(Request $request) {
$page = $request->has('page') ? $request->get('page') : 1;
$limit = $request->has('limit') ? $request->get('limit') : 10;
$books = Book::where('status', 0)->limit($limit)->offset(($page - 1) * $limit)->get()->toArray();
return $this->getResponse($books, count($books));
}
}
Maybe this
$products = $art->products->take($limit)->skip($offset)->get();