14
votes

I am new in laravel. My query is i need to find out value from comma separated field.

Here is my table:

tags_value

╔════╦══════════════╗
║ id ║     tags     ║
╠════╬══════════════╣
║  1 ║ css,html,php ║
║  2 ║ php,java,css ║
║  3 ║ java,c++,ios ║
╚════╩══════════════╝

This is my SQL query:

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

but it's not working.

Please help me to solve this problem. Thanks in advance.

4
Do you get any error?Mr. Engineer
i got this error Call to undefined function App\FIND_IN_SET()Ravi Kadia
Laravel thinks you are trying to call a function called FIND_IN_SET(), which does not exist. Escape the entire whereRaw clause in quotes.Tim Biegeleisen

4 Answers

34
votes

You need to escape the call to FIND_IN_SET() using quotes:

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

If you want to parameterize the column for which you search in FIND_IN_SET, then you can try something like this:

$colname = 'css'
$query = DB::table('tags_value')
         ->whereRaw('FIND_IN_SET(?,Tags)', [$colname])
         ->get();
7
votes

Try this :

->whereRaw("FIND_IN_SET('css',tags)")
1
votes
DB::table('tags_value')->whereRaw("find_in_set('php',tags)")....

Note that while using find_in_set() with Laravel Eloquent .find_in_set('.$tags.',columnName). sometime we become confuse that where we need give variable and db column name.

0
votes

How to use find in set in laravel with passing dynamic value to eloquent. 100% work.

$restaurants = Restaurant::whereRaw('FIND_IN_SET("'.$request->code.'",restaurant_code)')->get();