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.posts') }}
@if($search) @endif
@if($search)

@if($posts->total() > 0) {{ __('posts.search_results', ['count' => $posts->total(), 'query' => $search]) }} @else {{ __('posts.no_results', ['query' => $search]) }} @endif

@endif
@forelse($posts as $post) @empty
@if($search)

{{ __('posts.no_results', ['query' => $search]) }}

{{ __('posts.clear_search') }} @else

{{ __('posts.no_posts') }}

@endif
@endforelse
{{ $posts->links() }}