Controller for Laravel Live Search Box

<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use DB; class SearchController extends Controller {    public function index() { return view('search.search'); } public function search(Request $request) { if($request->ajax()) { $output=""; $products=DB::table('products')->where('title','LIKE','%'.$request->search."%")->get(); if($products) { foreach ($products as $key => $product) { $output.='<tr>'. '<td>'.$product->id.'</td>'. '<td>'.$product->title.'</td>'. '<td>'.$product->description.'</td>'. '<td>'.$product->price.'</td>'. '</tr>'; } return Response($output);    }    } } }
Here is a complete tutorial on creating your own live Laravel search ( https://www.cloudways.com/blog/live-search-laravel-ajax/ ) engine by using AJAX jQuery. A live search engine is one that shows results as user types the search query in it.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.