Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
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 :
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:
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.