I am creating a simple search form of my posts. It is returning the posts but ignoring the search term and it just displays all the posts. I see the correct term in the URL. Here is the function in the PostController
public function search(Request $request) {
// Get the search value from the request
$search = $request->input('search');
// Search in the title and body columns from the posts table
$posts = Post::query()
->where('title', 'LIKE', "%{$search}%")
->orWhere('body', 'LIKE', "%{$search}%")
->take(3)
->get();
// Return the search view with the resluts compacted
return view('search-results', compact('posts'));
}
Here is the search results
if($posts->isNotEmpty())
@foreach ($posts as $post)
<div class="post-list">
<p>{{ $post->title }}</p>
<img src="{{ $post->image }}">
</div>
@endforeach
@else
<div>
<h2>No posts found</h2>
</div>
@endif
This is the form
<form action="{{ route('search') }}" method="GET">
<input name="search" id="query" type="text" placeholder="search..." text>
<button type="submit">Search</button>
</form>
You should change the $request->input('search')
to $request->input('query')
. because your input name attribute is set to query