Tutorial – How to do a search form with laravel pagination bootstrap ?

Define routes at routes/web.php

//All certificates
Route::post('/search-certificates', 'CertificatesController@search' );
Route::get('/search-certificates', 'CertificatesController@search' );
Route::any('/search-certificates', 'CertificatesController@search' );

Define Controller function Search

public function search(Request $request){
    $searchString = $request->input('searchString');
    $searchCertificates = DB::table('NAMEOFYOURBABLE')
        ->where('BOROUGH', 'like', "%$searchString%" )
        ->orwhere('STREET', 'like', "%$searchString%" )
        ->orwhere('POSTCODE', 'like', "%$searchString%" )
        ->orderBy('POSTCODE', 'asc')
        ->orderBy('NUMBER', 'asc')
        ->orderBy('JOB_NUMBER', 'asc')

    $searchCertificates->appends(['searchString' => $searchString]);

    return view('search-certificates', ['searchCertificates' => $searchCertificates]);

Most important line on the code above is the one below because it will allow search pagination

    $searchCertificates->appends(['searchString' => $searchString]);

Define View Laravel Blade Page


<form action="/search-certificates" method="POST" role="search">
    {{ csrf_field() }}
    <div class="input-group">
        <input type="text" class="form-control" name="searchString"
               placeholder="Search by Street or Postal Code " />
         <button type="submit" class="btn btn-primary">Submit</button>


<div class="row justify-content-center">
<table class="table table-striped">
<th>Number of Units</th>
@foreach ($searchCertificates as $oneCertificate)
<td>{{ $oneCertificate->POSTCODE }}</td>
<td>{{ $oneCertificate->ISSUE_TYPE }}</td>
<td>{{ $oneCertificate->C_O_ISSUE_DATE }}</td>
<td> {{ $oneCertificate->BOROUGH }}</td>
<td>{{ $oneCertificate->NUMBER }}</td>
<td>{{ $oneCertificate->STREET }}</td>
<td>{{ $oneCertificate->PR_DWELLING_UNIT }}</td>
<td>{{ $oneCertificate->NTA }}</td>

Blade Pagination Link

{{ $searchCertificates->links( "pagination::bootstrap-4") }}

Author: David Raleche

Senior Technical Lead - Technologies used: CLOUD Amazon AWS/EBS, JIRA, Facebook Apps, Facebook Payment Engine, Facebook API, Perforce, Confluence,Laravel, Eloquent, ORM, PHP artisan, PSR-2, cloud9 ide, php artisan tinker, php artisan migration, Wordpress Implementing transition from Old facebook payment system to new payment system. Creating tools for the Customer Relationship department.