How to find the id in comma-separated values in Laravel?

In this tutorial, I will give you an example of “find the id in comma-separated values in Laravel”, so you can easily apply it to your Laravel 5, Laravel 6, Laravel 7, Laravel 8, Laravel 9, and Laravel 10 applications.

First, what we’re doing here, This is the example :

find the id in comma-separated values Laravel
find value in comma separated string or id in laravel
How To Search Comma Separated Values In Laravel
How to use 'where' on column with comma separated
Geting comma separated values as ID for another table

The requirement of finding the id or a value in comma-separated values in Laravel

In Laravel, if you have a column with comma-separated values and you want to find records based on a specific ID within those values, you can use the whereRaw or where methods to perform a query that searches for the ID in the comma-separated list. Here’s how you can do it using both methods:

Assuming you have a column named ids or tags that contains comma-separated values like “1,2,3,4”:

Using tags in posts in a Laravel application allows you to categorize and organize your posts with keywords or labels. Here’s a step-by-step guide on how to manage a tag system for posts or packages in Laravel:

Getting comma-separated values as ID for another table and Filter Posts or Packages by Tag in Laravel:

Controller File:

<?php
 
namespace App\Http\Controllers;
 
use Illuminate\Http\Request;
use App\Models\Package;
use App\Models\Tag;
 
class TagRelatedPackageController extends Controller
{
    public function viewTagPackage($tag)
    {
        #Get tag Id
        $tag = Tag::where('id',$tag->id)->first();

        #Get packages related to Tag Id
        $relatedTagPackages =  Package::select('id','package_name','thumbnail_image','price_to_show','slug','tag_pin','status')
            ->whereRaw('find_in_set("'.$tag->id.'",tag_pin)')
                ->where('status','1')
                    ->get();
        dd($relatedTagPackages);
    }
}

Route File:

<?php
 
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\TagRelatedPackageController;
 
 
Route::get('/tag/{tag}',[TagRelatedPackageController::class,'viewTagPackage']);



I hope that this article helped you learn how to find id in comma-separated values in Laravel. You may also want to check out our guide on How to Encrypt and Decrypt Strings in Laravel.

Hi, My name is Gaurav Pandey. I'm a Laravel developer, owner of 8Bityard. I live in Uttarakhand - India and I love to write tutorials and tips that can help other developers. I am a big fan of PHP, Javascript, JQuery, Laravel, WordPress. connect@8bityard.com

Scroll to Top