Laravel 10 Eloquent WHERE Like Query Example Tutorial

Last Updated on by in Laravel

This tutorial helps you learn how to use select like query in your laravel application.

Ideally, Laravel eloquent offers a query builder that allows us to tackle MySQL such a condition. This is very easy to use, or you could say its a no brainer.

All hail to “Query Builder” It can help you write Like query in Laravel and remember it is used with Where condition.

Laravel Eloquent, like the query, is mainly used to scour the dyad value of the table’s selected column.

So, you can do it with either of the method given below.

Like Query with WHERE Condition in Laravel with Query Builder

Let me explain to you by example; suppose you have a couple of country values in the countries (USA, UK, France, China) stored in your table. And you want to filter out France value.

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class CountryController extends Controller
{

    public function index()
    {
        $term = 'France';
        $filterData = DB::table('countries')->where('name','LIKE','%'.$term.'%')
                      ->get();
                      
        print_r($filterData);
    }

}

Like Query with Laravel Model

We can even execute the Like Query with Where clause using laravel model, here is the example that you must check on.

<?php
namespace App\Http\Controllers;

use App\Http\Controllers\Controller;
use App\Country;

class CountryController extends Controller
{

    public function index()
    {
        $term = 'France';
        $filterData = Country::table('countries')->where('name','LIKE','%'.$term.'%')
                      ->get();

        print_r($filterData);
    }

}

So this was it, we have completed the Laravel Like Where query tutorial.