簡體   English   中英

foreach() 參數必須是數組類型 | object, null (Laravel Livewire)

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

此代碼工作正常

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

但是當我嘗試使用 laravel livewire 進行分頁時,它給了我一個錯誤

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

刀片文件

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

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

在組件中導入這個

use Livewire\WithPagination;

class Product extends Component
{
    use WithPagination;

....

}

並在視圖中添加

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

哦,我明白了。實際上我已經使用 $products 變量作為全局變量,當我將 $products 更改為其他名稱時,它可以工作。 非常感謝...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM