'find in set in laravel ? example
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.
Solution 1:[1]
You need to escape the call to FIND_IN_SET()
using quotes:
$query = DB::table('tags_value')
->whereRaw('FIND_IN_SET("css", Tags)')
->get();
But unless it's a fixed value, you should always parameterize the column for which you search in FIND_IN_SET
:
$searchvalue = 'css';
$query = DB::table('tags_value')
->whereRaw('FIND_IN_SET(?, Tags)', [$searchvalue])
->get();
Solution 2:[2]
Try this :
->whereRaw("FIND_IN_SET('css',tags)")
Solution 3:[3]
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.
Solution 4:[4]
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();
Solution 5:[5]
This is how I have done it:
$value = '2';
Vendor::whereRaw('FIND_IN_SET("'.$value.'",service_industries)')->count();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | miken32 |
Solution 2 | Mr. Engineer |
Solution 3 | Syscall |
Solution 4 | Kaleemullah |
Solution 5 | hackernewbie |