resetPage(); } public function clearSearch(): void { $this->search = ''; $this->resetPage(); } public function highlightSearch(string $text, string $search): string { if (empty($search)) { return e($text); } $escapedText = e($text); $escapedSearch = e($search); return preg_replace( '/('.preg_quote($escapedSearch, '/').')/iu', '$1', $escapedText ); } public function with(): array { return [ 'posts' => Post::published() ->when($this->search, function ($query) { $search = $this->search; $query->where(function ($q) use ($search) { $q->where('title', 'like', "%{$search}%") ->orWhere('body', 'like', "%{$search}%"); }); }) ->latest() ->paginate(10), ]; } }; ?> {{ __('posts.search_placeholder') }} @if($search) @if($posts->total() > 0) {{ __('posts.search_results', ['count' => $posts->total(), 'query' => $search]) }} @else {{ __('posts.no_results', ['query' => $search]) }} @endif {{ __('posts.clear_search') }} @endif @if($posts->count() > 0) @foreach($posts as $post) @endforeach @elseif(! $search) {{ __('posts.no_posts') }} @endif {{ $posts->links() }}
@if($posts->total() > 0) {{ __('posts.search_results', ['count' => $posts->total(), 'query' => $search]) }} @else {{ __('posts.no_results', ['query' => $search]) }} @endif {{ __('posts.clear_search') }}
{{ __('posts.no_posts') }}