SQL Laravel Eloquent - Where field can be uppercase or lowercase
Stefan Bogdanescu
Founder & Senior Architect · 2026-06-29
Mastering Case Insensitivity in Laravel Eloquent: Handling Text Comparisons
As developers working with relational databases through frameworks like Laravel, one of the most common hurdles we encounter is dealing with string comparisons—specifically, ensuring our queries are case-insensitive. This issue arises because the exact behavior of case sensitivity often depends on the underlying database configuration and collation settings.
Let's dive into a practical scenario: searching for a plane_name where the input might be provided in any casing (Airbus, airbus, or AIRBUS). The default Eloquent query, while syntactically correct, might fail to retrieve results if the database is configured for strict case-sensitive matching.
The Case Sensitivity Challenge in SQL
The original query structure you presented looks like this:
$info = Plane::where('plane_name', '=', $pname)
->where('uid', '=', $uid)
->get();
If the database's collation is case-sensitive (which is common in many default setups), searching for 'airbus' will not match a record where the column stores 'Airbus'. To solve this, we need to force the comparison to ignore case during the database evaluation.
Solution 1: Leveraging SQL Functions with whereRaw
The most robust and portable way to achieve case-insensitive searching across various SQL dialects (MySQL, PostgreSQL, SQLite) is to use built-in string manipulation functions within your WHERE clause. The standard approach involves converting both the column value and the input parameter to a consistent case (usually lowercase or uppercase) before comparison.
In Laravel Eloquent, you can achieve this by using the whereRaw method, which allows you to inject raw SQL expressions directly into your query builder:
$pname = 'Airbus'; // Example input from user
$uid = 123;
// Convert both the column value and the input variable to lowercase for comparison
$info = Plane::whereRaw('LOWER(plane_name) = ?', [$pname])
->where('uid', '=', $uid)
->get();
Explanation:
LOWER(plane_name): This SQL function converts the value stored in theplane_namecolumn to all lowercase letters before the comparison happens.= ?: We use a placeholder (?) instead of concatenating the variable directly into the string. This is crucial for security, as it prevents SQL injection vulnerabilities. The actual values are passed separately as an array to thewhereRawmethod.
This approach ensures that no matter how the data is stored or how the user inputs the search term, the comparison operates purely on the lowercase representation, guaranteeing a case-insensitive match. This technique is fundamental when dealing with complex data retrieval in Laravel applications, much like when structuring relationships within https://laravelcompany.com.
Solution 2: Database Collation (The Structural Approach)
While using whereRaw solves the immediate query problem, it’s worth noting that the best long-term solution often lies in correctly configuring your database schema. If you are working with PostgreSQL or MySQL, ensuring that the plane_name column uses a case-insensitive collation (like utf8mb4_ci in MySQL) will make standard comparisons (=) inherently case-insensitive without needing to wrap every query in functions like LOWER().
However, relying solely on application logic (using whereRaw) provides a fallback mechanism that works reliably across almost all database systems.
Conclusion
When dealing with user input and data retrieval in Laravel Eloquent, treating string comparisons as operations that require explicit case handling is a best practice. By utilizing methods like whereRaw combined with SQL functions such as LOWER(), you gain complete control over your queries, ensuring they are robust, flexible, and case-insensitive regardless of the underlying database setup. Always prioritize security by using prepared statements (placeholders) whenever injecting dynamic values into raw SQL expressions, keeping the integrity of your application sound, whether you are building complex features or managing data models on https://laravelcompany.com.