Open the newly created SearchPosts.php
file and set up the properties and methods to manage the search query and results:
<?php
namespace App\Http\Livewire;
use Livewire\Component;
use App\Models\Post;
class SearchPosts extends Component
{
public $query = '';
public $posts = [];
public function updatedQuery()
{
$this->posts = Post::where('title', 'like', '%' . $this->query . '%')->get();
}
public function render()
{
return view('livewire.search-posts');
}
}