Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 972
Ravi Kadia Avatar asked Feb 24 '16 06:02

Ravi Kadia


People also ask

What is Find_in_set in laravel?

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.

What is 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.

What is Find_in_set in MySQL?

The FIND_IN_SET() function returns the position of a string within a list of strings.


2 Answers

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();
like image 87
Tim Biegeleisen Avatar answered Sep 19 '22 07:09

Tim Biegeleisen


Try this :

->whereRaw("FIND_IN_SET('css',tags)")
like image 25
Mr. Engineer Avatar answered Sep 22 '22 07:09

Mr. Engineer