Database LIKE condition

Submitted by editor on Tue, 12/08/2015 - 10:50
Question

How to use LIKE condition in database query on Drupal 8 ?

The like condition syntax is:
$query->condition('field_name', "%" . $query->escapeLike("The string") . "%", 'LIKE');

Example:

    $con = \Drupal\Core\Database\Database::getConnection();
    $query = $con->select('table_name', 't')
        ->fields('t', ['field_1', 'field_2']);
    $query->condition('field_1', "%" . $query->escapeLike($string) . "%", 'LIKE');
    $result = $query->execute()->fetchAll();

Add new comment

CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image.