How to search data from database in Laravel 7/6 | SQL Query for searching data from Laravel
You are in a right platform if you want to search data from database in Laravel . In this article we are going to see how you search data from your database table . We are not going to use any package for this , we will just use a single SQL Query and that's it .
While building a professional website search functionality is one of the key requirement of a customer and for developing a search functionality there several ways available like several packages available that you can use like Laravel Scout . But in this article we will search data without using any package . So let's start .
Step-1 : Setup your search bar :
First simply setup your search bar in your website where you want to search data . In this example i will be using my search bar in my " home.blade.php " .
project-name\resources\views\home.blade.php :
@extends('layouts.app') @section('content') <div class="container"> <div class="row justify-content-center"> <div class="col-md-8"> <div class="card"> <div class="card-body"> @if (session('status')) <div class="alert alert-success" role="alert"> {{ session('status') }} </div> @endif <form class="form-inline my-2 my-lg-2" method="get" action="{{url('/search')}}"> <input class="form-control mr-sm-2" type="search" placeholder="Search" aria-label="Search" style="width: 80%;" name="search"> <button class="btn btn-outline-success my-2 my-sm-0" type="submit">Search</button> </form> <table class="table table-dark"> <thead> <tr> <th scope="col">id</th> <th scope="col">Name</th> <th scope="col">Email</th> </tr> </thead> <tbody> @foreach($data as $value) <tr> <th scope="row">{{$value->id}}</th> <td>{{$value->name}}</td> <td>{{$value->email}}</td> </tr> @endforeach </tbody> </table> </div> </div> </div> </div> </div> @endsection
Step-2 : Set up Route ( web.php ) :
project-name\routes\web.php :
Setup your web.php file that after search where ( controller ) will you want to redirect your file .
<?php use Illuminate\Support\Facades\Route; Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/home', 'HomeController@index')->name('home'); Route::get('/search', 'HomeController@search');
Step-3 : Set up Controller :
In your respective controller write the following codes in order to perform search operation . In this example i will be using the HomeController .
project-name\app\Http\Controllers\HomeController.php :
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\User; use DB; class HomeController extends Controller { public function __construct() { $this->middleware('auth'); } public function index() { $data=User::all(); return view('home',compact('data')); } public function search(Request $req) { $search=$req->get('search'); $result=DB::table('users')->where('name','like','%'.$search.'%')->get(); return view('searchdata',compact('result')); } }
Step-4 : View search result :
project-name\resources\views\searchdata.blade.php :
@extends('layouts.app') @section('content') <div class="row container my-3"> <div class="col-2"></div> <div class="col-8 text-center"> <h2 class="text-center">this is the search result</h2> <table class="table table-dark"> <thead> <tr> <th scope="col">id</th> <th scope="col">Name</th> <th scope="col">Email</th> </tr> </thead> <tbody> @foreach($result as $value) <tr> <th scope="row">{{$value->id}}</th> <td>{{$value->name}}</td> <td>{{$value->email}}</td> </tr> @endforeach </tbody> </table> </div> <div class="col-2"></div> </div> @endsection
Output :
Thank you for reading this article 😊
For any query do not hesitate to comment 💬