0
votes

After adding Paginate function I'm getting this error

$sub_categories = SubCategory::where('id', $id)->first();
$products = Products::where('subcategory_id', $sub_categories->id)->get()->paginate(10);
1
Are you trying to get all categories and sub_categories, by any chance? - Qumber
I am trying to get all the products using subcategories. I have updated the code. - Naren
what are you trying to archive? you want to paginate $products? or $sub_categories - Andy Song
I am trying to get all products using subcategory @AndySong - Naren
check my updated answer. - Andy Song

1 Answers

1
votes

$sub_categories = SubCategory::where('id', $id)->first(), this will only give you null or one record, not a collection, so you can not use ->paginate(10); chain it. You will only get one record at the most, why you need to paginate?

Update so first for the sub_categories you do not need to paginate, as you just want one record. so the code should be like this.

$sub_categories = SubCategory::where('id', $id)->first();

second, if you want to paginate $products you should do this,

if ($sub_categories)
{
    $products = Products::where('subcategory_id', $sub_categories->id)->paginate(10);
}