The database query builder in Laravel provides a simple, intuitive interface for generating and performing database queries. It works with every one of Laravel's supported database systems and may be used to conduct most operations on the database in your application.

This article will teach you how to utilize select like queries in a Laravel application.

Laravel eloquent, in theory, provides a query builder that helps us to handle that kind of situation in MySQL. This is quite simple to use, or you could say a no-brainer.

"Query Builder" deserves a standing ovation. It can assist you in writing a Like query in Laravel, and keep in mind that it is utilized with the Where condition.

Like the query, Laravel Eloquent is mostly used to search the particular value of the table's selected column.

As a result, you can use one of the methods listed below.

1. WHERE Condition with Like Query in Laravel with Query Builder

Let me give you an example: imagine you have a couple of nation values in your table for the countries (USA, UK, France, and China). And you wish to exclude the worth of France.

<?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);
    }
}

2. Like Query with Laravel Model

We can also utilize the laravel model to perform the Like Query with Where clause; below is an example that you should look at.

<?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);
    }
}

I hope you will like the content and it will help you to learn Laravel Eloquent WHERE Like Query Example Tutorial
If you like this content, do share.


Recommended Posts

View All

Laravel CORS Example: How to Enable CORS in Laravel?


In this tutorial, i will teach you how to easily enable CORS (Cross-Origin Resource Sharing) in Laravel and work with it.

Laravel 9 CKeditor Image Upload With Example


install ckeditor laravel 9, ckeditor image upload not working laravel 9, how to upload image in ckeditor in laravel 9, how to upload image using ckedi...

Laravel Full Text Search with Scout


You will have learned how the scout elastic search functions from today's tutorial

Crop Image Before Upload Using Croppie.js in Laravel 9


Using Laravel 9, crop an image before uploading it using Croppie.js. Before uploading an image, Laravel uses Ajax to crop it with croppie. Cropping an...

Laravel Multiple Where Condition Example


Laravel multiple where condition, Laravel multiple where not working, Laravel multiple where same column, Laravel 8 Eloquent multiple where condition...