Amrinder Singh
Amrinder Singh

Reputation: 5502

How to search in comma separated value in Laravel 5.1?

I want to search in comma separated value from a column in database. I know how to do this in MySQL query,

In MySQL:

SELECT ... WHERE FIND_IN_SET('1', field)

but unable to find the alternative in Laravel.

here is what I have tried so far,

whereRaw(FIND_IN_SET(2,userid))

But it doesn't help.

Upvotes: 0

Views: 7497

Answers (4)

abhishek parmar
abhishek parmar

Reputation: 1

$q->whereIn('tablecolumn', function ($query) use ($YourArray) {
 $query->select(\DB::raw('tablecolumn'))
  ->from('YourTableName') 
  ->where(function ($query) use ($YourArray) {
   foreach ($YourArray as $value) {
    $query->orWhereRaw("FIND_IN_SET($value, tablecolumn)");
   }
 });
});

I found this solution and it works for me i have comma-seprated values in table and want to search by array values so it will be helpfull to you.

Upvotes: 0

Ramesh Adhikari
Ramesh Adhikari

Reputation: 31

Please Try the following code where $c is your search id like 1 or 2 or 3 etc and column_name is the your database table field name where search id store like 1,2,3 etc.

->whereRaw("find_in_set($c , column_name)");

means

->whereRaw("find_in_set('1' , column_name)");

Upvotes: 1

kush
kush

Reputation: 645

   // Receipt no.
    $fee_type_id = FeeType::select('id')->where('name', 'Main Fee')->first();

    // dd($fee_type_id);
    $receipt_group = FeeReceiptGroup::select('id', 'main_fee_status', 'receipt_group_id')->whereRaw('Find_IN_SET(?, fee_type_id)', [$fee_type_id->id])->first();

Upvotes: 1

user3489116
user3489116

Reputation:

Please try this:

->whereRaw('FIND_IN_SET(2,sent_mail_ids)')

this must help you

Upvotes: 3

Related Questions