Laravel Eloquent - How to query NOT LIKE?

Stefan Izdrail

Founder & Senior Architect · 2026-06-29

Laravel Company
Title: Laravel Eloquent - How to Query NOT LIKE? Body: In this comprehensive blog post, we will explore how to find something that is not like a certain string using Laravel Eloquent. Firstly, let's understand the basics and then move towards solving this query efficiently. Laravel Eloquent, as you might know, is an object-relational mapper in Laravel. It allows us to interact with database tables like objects instead of using complex SQL queries. By default, it provides a robust set of methods for common data manipulation tasks. However, there might be scenarios where we need to use customized queries which are not directly supported by Eloquent. To handle such cases, Laravel offers the Query Builder. The Query Builder is an expressive and intuitive way to work with database queries. It provides a fluent and flexible interface for constructing complex queries and facilitates advanced SQL operations like NOT LIKE query. So let's look at how we can achieve this using Eloquent models and the built-in Query Builder: 1. Using Laravel Eloquent model: - First, create an Eloquent model for your table or collection of data:

// Include the 'Model' namespace from Laravel:
use Illuminate\Database\Eloquent\Model;

class User extends Model {
  // Define table name and other relationships if needed
}
- Now, you can use this model for your queries. For example, let's say we want to find all users that do not have 'ray' in their usernames:

$unmatchedUsers = User::whereNotIn('username', function($query) {
  $query->select('username')
        ->from('users')
        ->whereRaw('username LIKE "%ray%"');
})->get();
- Here, we are using a nested query to find all users with 'ray' in their usernames and then excluding them from the final result set. This can be an expensive operation if you have large datasets. Remember that this method is still more efficient than executing raw SQL queries as it eliminates the need for manually iterating through your database records. 2. Using Laravel Eloquent scope: - Instead of using nested queries to filter out records, we can create a scope on our User model. Scopes are a convenient way to add common searches without repeating code in every query. To create this scope, we can define the following method:

public function scopeUnmatchedByRay($query) {
  $query->whereNotIn('username', function($query) {
    $query->select('username')
          ->from('users')
          ->whereRaw('username LIKE "%ray%"');
  });
}
- Now, you can use this scope in your queries:

$unmatchedUsers = User::unmatchedByRay()->get();
3. Using the Laravel Query Builder: - You can also find records that are not like a certain string using the Laravel Query Builder directly, without involving any models or scopes. Here's an example:

$unmatchedUsers = DB::table('users')
  ->select(DB::raw('*'))
  ->whereNotIn('username', function($query) {
    $query->select('username')
          ->from('users')
          ->whereRaw('username LIKE "%ray%"');
  })->get();
- However, this method may not be as convenient and efficient when working with large datasets. It is better to use the Eloquent model or scope approach if possible. In conclusion, Laravel provides multiple ways to query for records that are not like a certain string using its powerful features such as Eloquent models, scopes, and Query Builder. Choose the most suitable method depending on your requirements and preferences, always keeping efficiency, readability, and maintainability in mind. Remember, following best practices can make your code more concise, maintainable, and easy to scale with your project's growth. Check out https://laravelcompany.com/blog for further insights on Laravel and how it can boost your development outcomes.