Search Comma Separated values using FIND_IN_SET in Laravel Query Builder

Search Comma Separated values using FIND_IN_SET in Laravel Query Builder

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

article 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.

  1. $data = \DB::table("articles")
  2.      ->select("title")
  3.      ->whereRaw("find_in_set('php',tags)")
  4.      ->get();
Output :
Collection {#169 ▼
  #items: array:1 [▼
    0 => {#170 ▼
      +"title": "PHP Basic"
    }
  ]
}

Phone: (+91) 8800417876
Noida, 201301
Attention Required! | Cloudflare

Sorry, you have been blocked

You are unable to access ressim.net

Why have I been blocked?

This website is using a security service to protect itself from online attacks. The action you just performed triggered the security solution. There are several actions that could trigger this block including submitting a certain word or phrase, a SQL command or malformed data.

What can I do to resolve this?

You can email the site owner to let them know you were blocked. Please include what you were doing when this page came up and the Cloudflare Ray ID found at the bottom of this page.