find in set in laravel ? example

find in set in laravel ? example

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

Parsing Comma-Separated Fields in Laravel Without FIND_IN_SET

When working with legacy data or specific schema designs, you may encounter a table where multiple values are stored in a single column as a comma-separated string. The example provided shows a tags_value table where the tags column contains strings like "css,html,php". The immediate goal is often to filter records based on one of those tags without altering the database schema.

The query snippet you encountered attempts to use MySQL's FIND_IN_SET() function via Laravel's Eloquent or Query Builder:

$query = DB::table('tags_value')
         ->whereRaw(FIND_IN_SET('css', Tags))
         ->get();

This specific implementation fails primarily due to syntax errors in the SQL clause. The FIND_IN_SET() function requires two arguments: the value you are searching for and the column containing the comma-separated list. In your code, Tags is written as a bare word rather than a quoted column identifier. Inside Laravel's query builder, unquoted identifiers often get escaped or mishandled depending on the driver configuration, but more critically, the function call itself must be passed directly to whereRaw() with the correct argument order and proper quoting for the column name.

To fix this immediately, you need to adjust the SQL syntax so that Laravel passes the column name correctly:

$query = DB::table('tags_value')
         ->whereRaw("FIND_IN_SET('css', tags) > 0")
         ->get();

The condition > 0 is technically redundant if you are just checking for existence, as FIND_IN_SET returns 1 on a match and 0 otherwise. However, including it makes the intent explicit to anyone reading the raw SQL. A cleaner approach using Laravel's query builder is:

$query = DB::table('tags_value')
         ->whereRaw("FIND_IN_SET('css', tags) > 0")
         ->get();

Alternatively, if you prefer to avoid whereRaw for better maintainability and index usage (though FIND_IN_SET cannot use standard indexes), you can leverage a regular expression. This method searches for the tag surrounded by commas or at the start/end of the string to avoid partial matches:

$query = DB::table('tags_value')
         ->whereRaw("tags REGEXP '^css,|,css,|css$'")
         ->get();

The Long-Term Solution: Normalization

While FIND_IN_SET works for quick scripts or read-only reporting, it introduces significant technical debt. The database cannot utilize indexes on the tags column because the data is not normalized. Every time you run a query, the database engine must scan the entire string content of every row to find the comma-separated value. As your dataset grows, this will degrade performance linearly with the number of rows.

The industry standard for handling one-to-many relationships like tags is the normalization pattern. You should create a separate tag_model table and an intermediate pivot table (or use a polymorphic relation if appropriate) to link users or items to their tags. This transforms the schema from:

id tags
1 css,html,php

Into two tables:

tags:

id name
1 css
2 html
3 php

tag_model_items (pivot):

item_id tag_id
1 1
1 2
1 3

In Laravel, this allows you to use attach, detach, and standard eager loading techniques. You can define a many-to-many relationship in your Model:

// In your Item model
public function tags()
{
    return $this->belongsToMany(Tag::class);
}

Then querying becomes trivial, safe, and performant:

$items = App\Models\Item::whereHas('tags', function ($query) {
    $query->where('name', 'css');
})->get();

When to Stick with the Comma-Separated Approach

There are scenarios where you cannot refactor the database immediately. If you are maintaining a legacy system or dealing with a strict constraint that prevents schema changes, the FIND_IN_SET workaround is acceptable. However, always ensure you are using whereRaw with properly quoted column names to avoid syntax errors. Be aware that this approach limits your ability to sort by tags or use distinct counts efficiently without converting the data in application logic.

For any new development or refactoring projects, the normalized many-to-many relationship is the only robust path forward. It aligns with how Laravel's Eloquent is designed to handle relationships and ensures your application scales without requiring complex string manipulation queries.

Our network