0

I don't know if I correctly formulated the question. However, I've got 2 tables - products and categories (many to many relationship) and in categories page I want to display it's products with pagination if it has any and if it doesn't - I want to display "products not found". It worked, but without pagination.

Here's the controller:

public function show($slug)
{
    $category = $this->categoryRepository->findBySlug($slug);
    $paginate = $category->products()->paginate(6);

    return view('site.pages.category')->with(['category' => $category,
    'paginate' => $paginate]);
}

What to do achieve this?

  • 1
    Use `!empty($var);` or `isset($var);` – Justin E Jun 05 '20 at 13:49
  • 2
    Does this answer your question? [check if variable empty](https://stackoverflow.com/questions/8777591/check-if-variable-empty) – AndrewL64 Jun 05 '20 at 13:56
  • What do you mean by "empty"? undefined AKA `null`, empty string `""` or white-space string `" "` ? – Yousha Aleayoub Jun 05 '20 at 14:03
  • @JustinE I modified the controller like this: public function show($slug) { $category = $this->categoryRepository->findBySlug($slug); $products = $category->products(); if(isset($products) && !empty($products)){ $paginate = $products->paginate(6); return view('site.pages.category')->with(['category' => $category, 'paginate' => $paginate]); } else { return view('site.pages.category', compact('category')); } } However, now I get this: Call to a member function products() on null.. – Varg VIkernes Jun 05 '20 at 14:05

3 Answers3

0

Wrap it around an if statement like if(isset($your_variable) && !empty($your_variable)){ You may only need isset OR !empty, but you can mess around and see what works. Hope that helps!

  • I modified the controller like this: public function show($slug) { $category = $this->categoryRepository->findBySlug($slug); $products = $category->products(); if(isset($products) && !empty($products)){ $paginate = $products->paginate(6); return view('site.pages.category')->with(['category' => $category, 'paginate' => $paginate]); } else { return view('site.pages.category', compact('category')); } } However, now I get this: Call to a member function products() on null.. – Varg VIkernes Jun 05 '20 at 14:05
0

I modified the controller like this:

public function show($slug)
{
    $category = $this->categoryRepository->findBySlug($slug);
    $products = $category->products();

    if(isset($products) && !empty($products)){
    $paginate = $products->paginate(6);
    return view('site.pages.category')->with(['category' => $category,
    'paginate' => $paginate]);
    }
    else {
        return view('site.pages.category', compact('category'));
    }
}

However, now I get this: Call to a member function products() on null..

  • 1
    `$category` must be null here. – Justin E Jun 05 '20 at 14:11
  • @JustinE same error when changed to $category. I don't think that $category should be null.. I want to display both categories who have products and categories that don't. – Varg VIkernes Jun 05 '20 at 14:14
  • @VargVIkernes Just Debug through everyline. First, use `dd($category);` after the line `$category = $this->categoryRepository->findBySlug($slug);`. You won't be getting an object here. That's why you are getting the error `Call to a member function products() on null`. – Harish ST Jun 05 '20 at 15:24
0

Try this code below. I am not a Laravel Developer but just looked at some docs and writing this answer.

You are getting Call to a member function products() on null error, because $category is null (Not have any data). So you should check whether the $category have any data before calling products().

And you can check whether the products is empty or not in blade template and print the message accordingly. No need to render another template for that.

public function show($slug)
{
    $category = $this->categoryRepository->findBySlug($slug);

    if ($category) {
        $products = $category->products()->paginate(6);
    }

    return view('site.pages.category')->with(['products' => $products]);
}

And in your Blade Template, check the $products is not empty,

@if ($products)
    @foreach ($products as $product)
        {{ $product->name}}
    @endforeach
    {{ $products->links() }}
@else
    products not found
@endif

And obviously, change the template to your needs.

Harish ST
  • 1,475
  • 9
  • 23