---
description: >-
  https://www.itsolutionstuff.com/post/laravel-5-like-query-example-using-eloquent-where-clauseexample.html
---

# Laravel Like Query Example using Eloquent Where Clause (ok)



C:\xampp\htdocs\wpclidemo\database\seeders\DatabaseSeeder.php

```
<?php
namespace Database\Seeders;
use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder {
  /**
   * Seed the application's database.
   *
   * @return void
   */
  public function run() {
    \App\Models\User::factory(20)->create();
  }
}
```

**php artisan db:seed**

C:\xampp\htdocs\reset\routes\web.php

```
<?php
use App\Http\Controllers\HomeController;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
 */
Route::get('/', function () {
  return view('welcome');
});
Auth::routes();
Route::get('/home', [HomeController::class, 'index'])->name('home');
Route::get('/test', [HomeController::class, 'test'])->name('test');
```

C:\xampp\htdocs\reset\app\Http\Controllers\HomeController.php

```php
<?php
namespace App\Http\Controllers;
use App\Models\User;
class HomeController extends Controller {
  /**
   * Create a new controller instance.
   *
   * @return void
   */
  public function __construct() {
    // $this->middleware('auth');
  }
  /**
   * Show the application dashboard.
   *
   * @return \Illuminate\Contracts\Support\Renderable
   */
  public function index() {
    return view('home');
  }
  public function test() {
    $search = 'Li';
    $tests   = User::where('name', 'LIKE', "%{$search}%")->get();
    return view('tests')->with(compact('tests'));
  }
}
```

**Hoặc**

```php
<?php
namespace App\Http\Controllers;
use App\Models\User;
Use DB;
class HomeController extends Controller {
  /**
   * Create a new controller instance.
   *
   * @return void
   */
  public function __construct() {
    // $this->middleware('auth');
  }
  /**
   * Show the application dashboard.
   *
   * @return \Illuminate\Contracts\Support\Renderable
   */
  public function index() {
    return view('home');
  }
  public function test() {
    $search = 'a';
    // $tests   = User::where('name', 'LIKE', "%{$search}%")->get();
    $tests = $users = DB::table('users')->where('name', 'LIKE', "%{$search}%")->get();
    return view('tests')->with(compact('tests'));
  }
}
```

C:\xampp\htdocs\reset\resources\views\tests.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-header">Dashboard</div>
        <div class="card-body">
          @foreach ($tests as $i => $test)
            <p><span>{{ $i + 1 }}</span> {{ $test->name }}</p>
          @endforeach
        </div>
      </div>
    </div>
  </div>
</div>
@endsection
```

![](https://1592565776-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F-LVfVRgKBlsVnWvhnDaa%2Fuploads%2FXKwrWWcdm6jOXJw3cPHp%2FScreenshot_1.png?alt=media\&token=ee18c882-35a0-4015-943e-07e388b075b6)

Whenever you require to use select like query in your application then you can easily use. Laravel 5 provide query builder and mysql like condition is very simple to use. So let's see bellow example did convert bellow sql query.

**SQL Example:**

```
SELECT *FROM `users`WHERE `name` LIKE '%hdtopi%';
```

**Laravel Query Example:**

```
$search = 'hdtopi';$user = User::where('name','LIKE',"%{$search}%")              ->get();print_r($user);
```
