How to fetch data from the database using ‘like’ in CodeIgniter 4

The LIKE command is used to search for a specified pattern in a column.use of like in CodeIgniter 4 for fetch data use of like in CodeIgniter 4 for fetch dataexpoundsofttechsolution What is GMT? To use it in CodeIgniter 4 follow the few simple steps as explained below.

Step 1 Create a controller in app/controller directory ex. MyController.php.

Step 2 Now create a function inside above defined controller ex. MyFunction.php

<?php 

namespace App\Controllers;

use CodeIgniter\Controller;

class MyController extends Controller{

public function MyFunction{

//code here

}

}
Step 3 Now paste the below code inside the MyFunction.

$db = \Config\Database::connect();
      $builder = $db->table("table_name");
      $builder->like("Column_name", "%pattern%");
      $query = $builder->get();
      $result = $query->getResult();
      
      // echo "<pre>";
      // foreach($result as $Items):
        echo "<pre>";
      print_r ($Items);
The complete code will look like this

<?php 

namespace App\Controllers;

use CodeIgniter\Controller;

class MyController extends Controller{

public function MyFunction{

$db = \Config\Database::connect();
      $builder = $db->table("table_name");
      $builder->like("Column_name", "%pattern%");
      $query = $builder->get();
      $result = $query->getResult();
      
      // echo "<pre>";
      // foreach($result as $Items):
        echo "<pre>";
      print_r ($Items);

}

}
Explanation :
The first line is used to connect with database.
The second line define the table name of database from where data will be fetched.
The third line tells the column name of defined table and the pattern word define that what type of searching you want.