Skip to content

Instantly share code, notes, and snippets.

@antonioanerao
Last active October 2, 2023 22:49
Show Gist options
  • Select an option

  • Save antonioanerao/7e24216c601d2eb31b8a631ce83e25a6 to your computer and use it in GitHub Desktop.

Select an option

Save antonioanerao/7e24216c601d2eb31b8a631ce83e25a6 to your computer and use it in GitHub Desktop.
Laravel Livewire product component
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use Livewire\WithPagination;
class Product extends Component
{
use WithPagination;
protected $paginationTheme = 'bootstrap';
public string $searchQuery = '';
public int $currentPage;
public function render()
{
$this->searchQuery == '' ? $this->currentPage = 0 : $this->currentPage = 1;
$products = \App\Models\Product::when($this->searchQuery != '', function($query) {
$query->where('name', 'like', '%'.$this->searchQuery.'%');
})
->paginate(10, ['*'], 'page', $this->currentPage);
return view('livewire.product', compact('products'));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment