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.