Create Laravel Live Search Using Ajax

Today, We want to share with you Create Laravel Live Search Using Ajax.In this post we will show you Create Live Search Box In Laravel 5.8, hear for Ajax Live Search Table Generation in Laravel we will give you demo and example for implement.In this post, we will learn about Build a live search feature with Laravel and Vue.js with an example.

Create Laravel Live Search Using Ajax

There are the Following The simple About Create Laravel Live Search Using Ajax Full Information With Example and source code.

As I will cover this Post with live Working example to develop Laravel 5.8 Advance Live Search Box, so the laravel ajax get data from database for this example is following below.

Read Also:  jQuery Ajax POST example with Laravel 7/6

Step-1 Create the Laravel Controller

php artisan make:controller LiveSearch

Step-2 LiveSearch.php

Simple Go to app\Controller\LiveSearch.php

namespace App\Http\Controllers;
use Illuminate\Http\Request; 
use DB;
class LiveSearch extends Controller
{ 
function index()
{ 
	return view('search_instant_live'); 
} 
function action(Request $request) 
{ 
	if($request->ajax()) 
	{ 
		$output = ''; 
		$query = $request->get('query'); 
		if($query != '') 
		{ 
			$data = DB::table('tbl_member')->where('MemberName', 'like', '%'.$query.'%')
			->orWhere('Address', 'like', '%'.$query.'%')
			->orWhere('City', 'like', '%'.$query.'%')
			->orWhere('PostalCode', 'like', '%'.$query.'%')
			->orWhere('Country', 'like', '%'.$query.'%')
			->orderBy('MemberID', 'desc') ->get(); 
		} 
		else 
		{ 
		$data = DB::table('tbl_member')->orderBy('MemberID', 'desc')
		->get(); 
		} 
		$total_row = $data->count(); 
		if($total_row > 0) 
		{ 
			foreach($data as $row) 
			{ 
				$output .= ''.$row->MemberName.''.$row->Address.''.$row->City.''.$row->PostalCode.''.$row->Country.''; 
			} 
		} 
		else 
		{ 
			$output = 'No Data Found';
		} 
		$data = array( 'table_data' => $output, 'total_data' => $total_row ); 
		echo json_encode($data); 
	} 
} 
} 

Step-3 LiveSearch.blade.php

Read Also:  Laravel AJAX Live Search Example

Create a View File (LiveSearch.blade.php)

<!DOCTYPE html>
<html>
 <head>
  <title>Live search in laravel using AJAX - pakainfo.com</title>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css" />
  <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
 </head>
 <body>
  <br />
  <div class="container box">
   <h3 align="center">Live search in laravel using AJAX</h3><br />
   <div class="panel panel-default">
    <div class="panel-heading">Search Member Data</div>
    <div class="panel-body">
     <div class="form-group">
      <input type="text" name="search" id="search" class="form-control" placeholder="Search Member Data" />
     </div>
     <div class="table-responsive">
      <h3 align="center">Total Data : <span id="total_records"></span></h3>
      <table class="table table-striped table-bordered">
       <thead>
        <tr>
         <th>Member Name</th>
         <th>Address</th>
         <th>City</th>
         <th>Postal Code</th>
         <th>Country</th>
        </tr>
       </thead>
       <tbody>

       </tbody>
      </table>
     </div>
    </div>    
   </div>
  </div>
 </body>
</html>

<script>
$(document).ready(function(){

 fetch_member_data();

 function fetch_member_data(query = '')
 {
  $.ajax({
   url:"{{ route('search_instant_live.action') }}",
   method:'GET',
   data:{query:query},
   dataType:'json',
   success:function(data)
   {
    $('tbody').html(data.table_data);
    $('#total_records').text(data.total_data);
   }
  })
 }

 $(document).on('keyup', '#search', function(){
  var query = $(this).val();
  fetch_member_data(query);
 });
});
</script>

Step- 4 Create View And define Controller

Read Also:  laravel 6 Retrieve data using jQuery Ajax

Laravel Set the Routes For display or View as well as Controller

Route::get('/search_instant_live', '[email protected]'); Route::get('/search_instant_live/action', '[email protected]')->name('search_instant_live.action');

Angular 6 CRUD Operations Application Tutorials

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about Create Laravel Live Search Using Ajax.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1. Read Also:  Laravel 5.8 Autocomplete Textbox Fields Using jQuery
  2. Read Also:  Laravel 7 jQuery Ajax Request Tutorial
  3. Read Also:  jQuery Ajax Delete Request with Laravel
  4. Read Also:  PHP Remove File from Folder and Database
  5. Read Also:  Laravel 6 JQuery Ajax Post Request Example
  6. Read Also:  Simple Eloquent Laravel One to One Relationship
  7. Read Also:  Laravel 5.7 Google reCAPTCHA Tutorial with Example
  8. Read Also:  Angular 6 applications - Insert, Fetch , Edit - update , Delete Operations
  9. Read Also:  Angular 6 Form Validation with Message - Angular 6 Live Validation
  10. Read Also:  Get Gravatar Image Using PHP Example
CLOSEX