Home > Back-end >  foreach() argument must be of type array | object, null given (Laravel Livewire)
foreach() argument must be of type array | object, null given (Laravel Livewire)

Time:01-25

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

CodePudding user response:

import this in Component

use Livewire\WithPagination;

class Product extends Component
{
    use WithPagination;

....

}

and add in view

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

CodePudding user response:

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...

  •  Tags:  
  • Related