motazdev's avatar

Pagination with findorfail

hey guys, i'm trying to paginate a products of specific category route.

$category = Category::with('products')->findOrFail($id)->paginate(12);

but it gives me that error

Undefined property: Illuminate\Pagination\LengthAwarePaginator::$category

how to solve this please?

0 likes
8 replies
tykus's avatar

What do you think you are paginating exactly?

motazdev's avatar

i'm getting a products of specific category but i cant paginate in this products

tykus's avatar

This is as efficient as eager-loading.

$category = Category::findOrFail($id)
$products = $category->products()->paginate(12);
1 like
martinbean's avatar

@motazdev findOrFail returns a single model instance. So how are you intending to “paginate” a single model?

2 likes
jlrdw's avatar

Find $category first, then attach and paginate.

1 like
motazdev's avatar

please can you give me any example? cause i think i'm doing what you told me

Please or to participate in this conversation.