How to Search Comma Separated Values in Laravel

Websolutionstuff | Sep-15-2021 | Categories : Laravel

Today, in this post i will show you how to search comma separated values in laravel. Here, we will find specific id from comma separated value 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 predefine function of php mysql, here, we will use FIND_IN_SET() with whereRow() of laravel query builder for laravel comma separated search example. So let's see how to search comma separated values in laravel.

First of al we need to create 2 tables for find specific id from comma separated value. I have posts and tags tables in my website.

1) In posts table we have "ID", "name", "tag_id" 

2) In tags table we have "ID", "name"

Here, we will find records that have ID "1" from "tag_id" column of 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 below.

Illuminate\Support\Collection Object
(
    [items:protected] => Array
    (
        [0] => stdClass Object
            (
                [id] => 1
                [name] => post1
                [tag_id] => 1,2
            )

        [1] => stdClass Object
            (
                [id] => 2
                [name] => post2
                [tag_id] => 1
            )
    )
)

 


 

You might also like :

Recommended Post
Featured Post
How To Render Charts In React: using react-chartjs-2 and Chart.js
How To Render Charts In React:...

​​React is a really cool tool that helps programmers make awesome user interfaces using JavaScript.  When it com...

Read More

Jul-26-2023

Laravel 8 CRUD Operation Example
Laravel 8 CRUD Operation Examp...

In this article, we will see the laravel 8 crud operation. As you know Laravel 8 has already been officially released an...

Read More

Sep-16-2020

Laravel 8 Remove/Hide Columns While Export Data In Datatables
Laravel 8 Remove/Hide Columns...

In this article, we will see how to remove/hide columns while export data in datatables in laravel 8. When we are u...

Read More

Oct-13-2020

Laravel 11 Livewire Dependent Dropdown
Laravel 11 Livewire Dependent...

Hello, laravel web developers! In this article, we'll see how to create a dependent dropdown in laravel 11 Livewire....

Read More

Jun-03-2024