Laravel Like Query Operator in Where Clause with Example

Learn Laravel Like Query on how to search keywords in your records and retrieve the results you need to your front end.

Created on: Jul 12, 2022
1,473
Laravel Like Query Operator in Where Clause with Example

In this tutorial, we will tackle Laravel Like Query where clause using the like operator.

The Laravel Like Query is very useful when you are searching for a word or part of a word or paragraph in the column value.

For example, you are searching for users having john on their name.

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;

class HomeController extends Controller
{

	/**
    * Display a listing of the resource.
    *
    * @return \Illuminate\Http\Response
    */
    public function index()
    {
        $users = User::where('name', 'like', '%john%')->get();

        dd($users);
    }
}

%john% - searches exact value, the part between, part at the beginning, and part at the end.
%john - exact value, part at the end
john% - exact value, part at the beginning

I hope it helps you understand more about Laravel like() operator in where clause.

That's pretty much it.

Leave a Comment