PCS開発チーム
PCS開発チーム
Livewire Computedの使い所は検索機能
namespace App\Livewire;
use App\Models\Post;
use Illuminate\Contracts\Pagination\Paginator;
use Illuminate\Database\Eloquent\Builder;
use Livewire\Attributes\Computed;
use Livewire\Component;
use Livewire\WithPagination;
class Posts extends Component
{
use WithPagination;
public string $search = '';
#[Computed]
public function posts(): Paginator
{
return Post::query()
->whereAny(['title', 'content'], 'like', "%$this->search%")
->latest()
->paginate();
}
// render()は省略可。
}
inputへの入力に応じて検索結果がリアルタイムに更新される。
<input type="text" wire:model.live="search">
@foreach($this->posts as $post)
@endforeach