How to Implement Category Search in Laravel
Thank you for your continued support.
This article contains advertisements that help fund our operations.
Table Of Contents
関連動画
Related Video
This is a video where we actually tried out the content from the article! If anything is unclear in the article, please check out the video.
The video provides further explanations and demonstrations, so it should be helpful.
Subscribe to Our Channel
If you found this video helpful, please consider subscribing to our channel or giving it a thumbs up! It really motivates us to create more content.
Questions and Feedback
If you have any questions or feedback regarding this article or the video, feel free to leave them in the comment section of the video. Your input is greatly appreciated and will help us improve our content in the future!
This article summarizes how to implement category search in Laravel.
Searching Using Categories
Previously, we added a category selection feature when creating posts. This time, we will display a list of posts filtered by the selected category.
How to Implement a Category Feature in Laravel
Creating a List Page Using Path Parameters
routes/web.php
Route::get('/category/{category}', [PostController::class, 'category'])->name('category');
PostController
We previously retrieved the list for the top page. Now, let’s add a filter as shown below.
public function category($category)
{
$posts = Post::with('user')
->with('category')
// Add filter here
->whereHas('category', function($q) use ($category) {
$q->where('name', $category);
})
// End of filter
->orderBy('id', 'desc')
->take(10)
->get();
return view('post/category', compact('posts'));
}
The whereHas
function allows you to filter based on a related model.
Laravel 11.x Eloquent: Relationships
resouces/views/post/category.blade.php
@foreach($posts as $post)
<div class="max-w-sm rounded mt-4 overflow-hidden shadow-lg">
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">
<a href="{{ route('post.show', ['postId' => $post->id]) }}"
>{{ $post->title }}</a
>
</div>
<p>{{ $post->category->name }}</p>
<p class="text-gray-700 text-base">{{ $post->user->name }}</p>
<p class="text-gray-700 text-base">{{ $post->content }}</p>
</div>
</div>
@endforeach
This will display the list.
Access localhost/category/Category1
or a category you created to check if it works.
Adding Links
Once the category list page is ready, create links to navigate to it.
@foreach($posts as $post)
<a href="{{ route('category', ['category' => $post->category->name]) }}"
>{{ $post->category->name }}</a
>
@endforeach
Dynamically add routing to link to the category list.
Implementing with Query Parameters
routes/web.php
Route::get('/posts', [PostController::class, 'search'])->name('post.search');
PostController
Simply add the first line, and the rest can be implemented similarly to the path parameter approach.
public function search(Request $request)
{
$category = $request->category;
$posts = Post::with('user')
->with('category') // Add this
->whereHas('category', function($q) use ($category) {
$q->where('name', $category);
})
->orderBy('id', 'desc')
->take(10)
->get();
return view('post/search', compact('posts'));
}
resouces/views/post/search.blade.php
This is identical to the path parameter approach.
@foreach($posts as $post)
<div class="max-w-sm rounded mt-4 overflow-hidden shadow-lg">
<div class="px-6 py-4">
<div class="font-bold text-xl mb-2">
<a href="{{ route('post.show', ['postId' => $post->id]) }}"
>{{ $post->title }}</a
>
</div>
<p>{{ $post->category->name }}</p>
<p class="text-gray-700 text-base">{{ $post->user->name }}</p>
<p class="text-gray-700 text-base">{{ $post->content }}</p>
</div>
</div>
@endforeach
Adding Links
Linking to the query parameter route is done similarly.
@foreach($posts as $post)
<a href="{{ route('post.search', ['category' => $post->category->name]) }}"
>{{ $post->category->name }}</a
>
@endforeach
Which Approach Is Best?
It depends on the situation.
There are various considerations, such as maintenance cost and techniques for SEO optimization. Personally, I prefer focusing on ease of maintenance by avoiding the creation of redundant controllers or views.
Understanding "database-driven SEO" is also crucial.
If a simple category list page is sufficient, the path parameter approach is fine.
For scenarios where filtering is expected on the destination page, implementing query parameters from the start may be quicker. Planning for future updates is also a valid consideration.
Some businesses create both category list pages and search pages.
I hope this is helpful to someone.