In this article, we will see how to search comma separated values in laravel. Here, we will find specific id from comma separated values and will get comma separated values from the table. Here, we will search comma separated values using FIND_IN_SET() function.
FIND_IN_SET() is a predefined function of PHP MySQL. Also, we will use FIND_IN_SET() with whereRow() of the laravel query builder for the laravel comma separated search example.
So, let's see how to find comma separated values in laravel 8, and get comma separated values in laravel.
First of all, we need to create 2 tables to find specific id from comma separated values. I have posts and tags tables on my website.
In the posts table we have "ID", "name", "tag_id".
In the tags table we have "ID", "name".
Here, we will find records that have ID "1" from the "tag_id" column of the posts table.
$search_id = 1; $data = \DB::table("posts") ->select("posts.*") ->whereRaw("find_in_set('".$search_id."',posts.tag_id)") ->get();
And you will get output like the below.
Illuminate\Support\Collection Object ( [items:protected] => Array (  => stdClass Object ( [id] => 1 [name] => post1 [tag_id] => 1,2 )  => stdClass Object ( [id] => 2 [name] => post2 [tag_id] => 1 ) ) )
You might also like: