Search comma separated value using find_in_set() in Laravel 5 Example


Here is a crazy example today, how to use find_in_set() mysql function in laravel query builder. If you need to add tags or something that store as comma separated values in database column, then you can simply store but if also need to search then it can be hard.

Like if you have posts table with tags column you simply store data like "laravel,php,html" etc comma separated string then how you found records only tags belong to laravel. So at that moment we can use mysql find_in_set() and we can use this function using whereRaw() of laravel query builder. So let's bellow bellow syntax and example.

Syntax:

FIND_IN_SET(needle,haystack);

Example:

$data = \DB::table("posts")

->select("posts")

->whereRaw("find_in_set('laravel',tags)")

->get();

I hope you found your best.....


July 9, 2017 | 996 Viewer | Category : PHP Laravel

Related Posts: