Search Comma Separated values using FIND_IN_SET in Laravel Query Builder
In this post we will give you information about Search Comma Separated values using FIND_IN_SET in Laravel Query Builder. Hear we will give you detail about Search Comma Separated values using FIND_IN_SET in Laravel Query BuilderAnd how to use it also give you demo for it if it is necessary.
In this post, I will tell you how to search comma separated value in Laravel Query Builder using MySQL find_in_set()
method.
Using find_in_set()
method, you can find the position of a string within a comma separated value.
Syntax
FIND_IN_SET(needle,haystack);
Ok, Let’s have a table which stores the article with the tags and you need to find out value from comma separated tags.
articles table
Now if you need to find out the articles that tagged into “php”, you can use the “FIND_IN_SET” method in following way :
SELECT title FROM articles WHERE FIND_IN_SET('php', tags);
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.
- $data=DB::table("articles")
- ->select("title")
- ->whereRaw("find_in_set('php',tags)")
- ->get();
$data = DB::table("articles") ->select("title") ->whereRaw("find_in_set('php',tags)") ->get();
Output :
Collection {#169 ▼ #items: array:1 [▼ 0 => {#170 ▼ +"title": "PHP Basic" } ] }
However you need to find out the results from comma separated values then you can get the idea from above queries. If you face any issue then please feel free to post your queries.
Hope this code and post will helped you for implement Search Comma Separated values using FIND_IN_SET in Laravel Query Builder. if you need any help or any feedback give it in comment section or you have good idea about this post you can give it comment section. Your comment will help us for help you more and improve us. we will give you this type of more interesting post in featured also so, For more interesting post and code Keep reading our blogs