Search code examples
laravellaravel-livewire

foreach() argument must be of type array | object, null given (Laravel Livewire)


This code is working fine

public function render(){
    $this->products = ProductModel::get();
    return view('livewire.product');
}

But when I am trying to paginate using laravel livewire, it gives me an error

public function render(){
     return view('livewire.product', [
         'products' => ProductModel::paginate(10)
    ]);
}

Blade File

@foreach ($products as $product)
  {{ $product->name }}
  {{ $product->price }}
@endforeach

@if(!empty($products))
{{ $products->links() }}
@endif

Solution

  • Ohh I got it.Actually I have already use $products variable as a global variable and when I change $products to other name it works. Thanks alot...