Adding Blog Search in Laravel 5+
A must-have functionality on every blog the ability to search articles via title or content, and in this article, we focus on adding Search Engine, the easiest way.
The idea we used here can also be used in other search engines, like product or catalog search, profiles and etc.
Let’s get started.
partials/_sidebar.blade.php
We’ll add a search form in section
or add it in your own choice.
<div class="sidebar-module">
<h4>Search</h4>
<form action="/search/" method="GET">
<input type="text" name="s" value="{{ Request::query('s') }}" placeholder="Search this site..." />
</form>
</div>
routes/web.php
Here’s our updated routes
, now with search
route.
...
Route::get('/', 'PagesController@getIndex')->name('home');
// Making sure our search term does only contains word and digit
Route::get('search/{s?}', 'SearchesController@getIndex')->where('s', '[\w\d]+');
Route::get('article/{slug}', 'ArticlesController@getSingle')->name('single');
Route::get('articles', 'ArticlesController@getIndex');
Route::resource('pages', 'PagesController');
Route::resource('posts', 'PostsController');
Route::resource('categories', 'CategoriesController');
Route::resource('comments', 'CommentsController');
Route::post('comments/{comment}/approve', 'CommentsController@approveComment')->name('comment.approve');
Route::post('comments/{comment}/unapprove', 'CommentsController@unapproveComment')->name('comment.unapprove');
Auth::routes();
Controller
Let’s add new controller
just for search.
php artisan make:controller SearchesController
SearchesController.php
This controller will be used to query blog posts from the Database.
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Post;
class SearchesController extends Controller
{
public function getIndex( Request $request ) {
$s = $request->query('s');
// Query and paginate result
$posts = Post::where('post_title', 'like', "%$s%")
->orWhere('post_content', 'like', "%$s%")
->paginate(6);
return view('searches.index', ['posts' => $posts, 's' => $s ]);
}
}
searches/index.blade.php
Create a new searches/
views and a blade file called index.blade.php
in it, we’ll use this View to display the search result.
@extends('main')
@section('title')
| Search on {{ $s }}
@endsection
@section('content')
<div class="blog-header">
<h1 class="blog-title">Searching for "{{ $s }}"</h1>
<p>We've found {{ $posts->count() }} results for your search term in all blog entries</p>
</div>
<div class="row">
<div class="col-sm-8 blog-main">
@if( $posts->count() )
@foreach( $posts as $post )
<div class="blog-post">
<h2 class="blog-post-title">
<a href="/article/{{ $post->post_slug }}">{{ $post->post_title }}</a>
</h2>
<p class="blog-post-meta">{{ date('M j, Y', strtotime( $post->created_at )) }} by <a href="#">{{ Helper::get_userinfo( $post->author_ID )->name }}</a></p>
<div class="blog-content">
{{--If post content is > 200 in characters display 200 only or else display the whole content--}}
{{ strlen( $post->post_content ) > 200 ? substr( $post->post_content, 0, 200) . ' ...' : $post->post_content }}
</div>
</div>
@endforeach
@else
<p>No post martch on your term <strong>{{ $s }}</strong></p>
@endif
{{-- Display pagination only if more than the required pagination --}}
@if( $posts->total() > 6 )
<nav>
<ul class="pager">
@if( $posts->firstItem() > 1 )
<li><a href="{{ $posts->previousPageUrl() }}">Previous</a></li>
@endif
@if( $posts->lastItem() < $posts->total() )
<li><a href="{{ $posts->nextPageUrl() }}">Next</a></li>
@endif
</ul>
</nav>
@endif
</div>
</div>
@endsection
That’s it, happy coding ^_^