How to Search Comma Separated Values in Laravel

How to Search Comma Separated Values in Laravel
How to Search Comma Separated Values in Laravel

Hello friends, Today we will show you how to search comma separated values in laravel. Sometimes, we need to manage to take single column for multiple integer ids, for example if we have posts table and need to set multiple tags on each post, so at that time we can take just single column and store selected tags ids.

Read Also :  Laravel 7 Login with Linkedin using Socialite

Here, In this example we will use FIND_IN_SET() function for search comma separated values. 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.

Search Comma Separated Values in Laravel

$search_id = 1;
$data = \DB::table("posts")
    ->select("posts.*")
    ->whereRaw("find_in_set('".$search_id."',posts.tag_id)")
    ->get();

I hope you understand of How to Search Comma Separated Values and it can help you…

Read Also :  Laravel 8 Soft Delete Example