Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
219 views
Welcome To Ask or Share your Answers For Others

1 Answer

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();

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...