Related content

Category : #laravel

Tags : #laravel, #laravel search

There are many search options in laravel like algolia search, meily search, tnt search and so on. But assume we have a simple model that just contains name and email and we want to search data in this model. In this case, no need to use those search options. We can implement a simple search option in this case. 

In this tutorial, I will show you, how to implement a simple search option in laravel 10 applications. I will use a user model to create this search functionality in laravel 10. We will use like query or like operator to create a search in laravel eloquent.

Let's see the preview of laravel search form:


Step 1: Install Laravel

First of all, we need to get a fresh Laravel 10 version application using the bellow command, So open your terminal OR command prompt and run the bellow command to start laravel search query like tutorial.

composer create-project laravel/laravel example-app


Step 2: Connect Database

After successfully installing the laravel app and then configuring the database setup. We will open the ".env" file and change the database name, username, and password in the env file to create how to implement search in laravel.




Read also: Laravel 10 Import Large CSV File Using Queue


Step 3: Create Migration and Model

In this step, we need to create users table and model. then we need to run a migration. so let's change the files.



use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

return new class extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('users', function (Blueprint $table) {
            $table->string('profile_photo_path', 2048)->nullable();

     * Reverse the migrations.
     * @return void
    public function down()


Now update the user model by replacing it with the below code:



namespace App\Models;

use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Notifications\Notifiable;
use Laravel\Fortify\TwoFactorAuthenticatable;
use Laravel\Jetstream\HasProfilePhoto;
use Laravel\Sanctum\HasApiTokens;

class User extends Authenticatable
    use HasApiTokens;
    use HasFactory;
    use HasProfilePhoto;
    use Notifiable;
    use TwoFactorAuthenticatable;

     * The attributes that are mass assignable.
     * @var string[]
    protected $fillable = [

     * The attributes that should be hidden for serialization.
     * @var array
    protected $hidden = [

     * The attributes that should be cast.
     * @var array
    protected $casts = [
        'email_verified_at' => 'datetime',

     * The accessors to append to the model's array form.
     * @var array
    protected $appends = [


Now you have to run this migration by following the command:

php artisan migrate


Now update the seeder class like below:



namespace Database\Seeders;

// use Illuminate\Database\Console\Seeds\WithoutModelEvents;
use Illuminate\Database\Seeder;

class DatabaseSeeder extends Seeder
     * Seed the application's database.
     * @return void
    public function run()


Now run the below command to insert some dummy data.

php artisan db:seed


Read also: Laravel 9 Cursor Pagination Example


Step 4: Create Route

Here, we need to add one route to display the users data and make a search functionality in laravel 10. So add it like below:



use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TutorialController;

Route::get('/', [TutorialController::class,'index'])->name('index');


Step 5: Create Controller

Here, we need to add the index() method for fetching users data with laravel search query in TutorialController. so let's add like as below:



namespace App\Http\Controllers;

use App\Models\User;
use App\Jobs\ProcessCSVData;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Bus;
use Illuminate\Database\Eloquent\Builder;

class TutorialController extends Controller
    public function index(Request $request)
        $users = User::query()
                function (Builder $builder) use ($request) {
                    $builder->where('name', 'like', "%{$request->q}%")
                        ->orWhere('email', 'like', "%{$request->q}%");

        return view('welcome', compact('users'));


Step 6: Create Blade file

In this step, we need to create a welcome blade file and update it like below. so let's change it.



<div class="container">
    <div class="row justify-content-center">
        <div class="col-md-8">
            <div class="card">
                <div class="card-header">Laravel 10 Simple Search With Like Operator - Laravelia</div>
                 <div class="card-body">
                    <form action="{{ route('index') }}" method="get">
                        <div class="row">
                            <div class="col-md-10">
                                <input type="text" class="form-control mb-3" placeholder="search" name="q">
                            <div class="col-md-2">
                                <input type="submit" class="form-control mb-3" value="Search">
                    <table style="width: 100%">
                            @foreach($users as $user)
                                <td>{{ $loop->index + 1 }}</td>
                                <td>{{ $user->name }}</td>
                                <td>{{ $user->email }}</td>
                                <td>{{ $user->status == 'active' ? 'Active' : 'Inactive'}}</td>
                    <center class="mt-5">
                        {{  $users->withQueryString()->links() }}


Now create an app blade file and update it like this: 


<!doctype html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- CSRF Token -->
    <meta name="csrf-token" content="{{ csrf_token() }}">

    <title>{{ config('', 'Laravel') }}</title>
    <!-- Tailwindcss -->
    <script src=""></script>

    <!-- Scripts -->
    @vite(['resources/sass/app.scss', 'resources/js/app.js'])
    <div id="app">
        <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm">
            <div class="container">
                <a class="navbar-brand" href="{{ url('/') }}">
                <button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}">
                    <span class="navbar-toggler-icon"></span>

                <div class="collapse navbar-collapse" id="navbarSupportedContent">
                    <!-- Left Side Of Navbar -->
                    <ul class="navbar-nav me-auto">


                    <!-- Right Side Of Navbar -->
                    <ul class="navbar-nav ms-auto">
                        <!-- Authentication Links -->
                            @if (Route::has('login'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a>

                            @if (Route::has('register'))
                                <li class="nav-item">
                                    <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a>
                            <li class="nav-item dropdown">
                                <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-bs-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre>
                                    {{ Auth::user()->name }}

                                <div class="dropdown-menu dropdown-menu-end" aria-labelledby="navbarDropdown">
                                    <a class="dropdown-item" href="{{ route('logout') }}"
                                        {{ __('Logout') }}

                                    <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none">

        <main class="py-4">


Read also: How To Create Pagination From Laravel Collection?


Ok, now we are ready to go and test search in laravel eloquent tutorial. So let's run the project using this command:

php artisan serve


Now you can test our application by visiting the below URL




Now we know how to implement search in laravel. Hope this search functionality in laravel 10 tutorial will help you to create search in laravel eloquent.