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.
FIND_IN_SET method is predefine method of MySQL. In Laravel, You will use whereRaw() method to write your own raw SQL queries. So you can use “find_in_set” method with whereRaw() in Laravel.
WhereRaw() is a function of Laravel query builder which puts your input as it is in the SQL query's where clause. Think of it as the where() function whose input argument will not be processed before inserting into queries.
The FIND_IN_SET() function returns the position of a string within a list of strings.
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();
Try this :
->whereRaw("FIND_IN_SET('css',tags)")
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With