Find in Set in Laravel? Example

find in set in laravel ? example

You need to escape the call to FIND_IN_SET() using quotes:

$query = DB::table('tags_value')
->whereRaw('FIND_IN_SET("css", Tags)')
->get();

But unless it's a fixed value, you should always parameterize the column for which you search in FIND_IN_SET:

$searchvalue = 'css';
$query = DB::table('tags_value')
->whereRaw('FIND_IN_SET(?, Tags)', [$searchvalue])
->get();

Using find_in_set() in laravel where clause

I see several potential problems with your current code. First, you should be binding the $q_category value to the call to FIND_IN_SET. Second, the check for a successful call to FIND_IN_SET is that the return value is greater than zero, as opposed to non NULL.

$q_category = 8;
$object = DB::table("questions")
->select('id')
->whereRaw("FIND_IN_SET(?, categories) > 0", [$q_category])
->get();

How to get last updated record with FIND_IN_SET in laravel?

There were several typos in your query. Instead of adding everything into a DB::select(), I also cleaned your query a bit:

return DB::table('basic_info')
->select('basic_info.updated_at', 'basic_info.updated_by', 'basic_info.id')
->join('emp_info', 'basic.emp_master_id', 'emp_info.emp_id')
->whereRaw("basic_info.id IN (SELECT basic_info.id FROM basic_info WHERE basic_info.updated_at = (SELECT MAX(basic_info.updated_at) FROM basic_info) AND basic_info.team_id = '1' AND FIND_IN_SET('18',emp_details.emp_grp) ORDER BY basic_info.id DESC LIMIT 1)")
->get();

Would the following work for you?

How to use NOT FIND_IN_SET in Laravel 5.1?

whereRaw() allows you to add any arbitrary SQL code to your query - in order to reverse the constraint simply use the same query you'd use in raw SQL:

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

or another variation of this constraint:

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

How to search from multiple columns in laravel with comma separated values

If the main_category is VARCHAR or similar, you need to do this:

Company::where(function($q) use($categoryId) {
$q->where('main_category', 'like', '%,' . $categoryId . ',%')
$q->orWhere('main_category', 'like', $categoryId . ',%')
$q->orWhere('main_category', 'like', '%,' . $categoryId)
})
->where($matchThese)->get();

But a much better way to handle this is to use many to many relationship between Company and Category models. You need to create a new pivot table category_company and use it as described in the docs.

FIND_IN_SET with two strings

$skills = 'select the employee skills';
$skl_arr = explode(',',$skills);
$skl_length = count($skl_arr);

/*query */

$rows->orwhere(DB::raw("find_in_set('$skl_arr[0]','post_job.skills')"));

for ($i=1; $i < $skl_length ; $i++) {
$rows->$join->on(DB::raw("find_in_set('$skl_arr[$i]','post_job.skills')",DB::raw(''),DB::raw('')));

}

How to find Array values to search in FIND_IN_SET

You want find all the size Ids matching with the $input['size_id'] and this is an array you can do something like

  $offer = Offer::whereHas('ApplyOn', function ($query) use ($input) {
if (isset($input['size_id']) and is_array($input['size_id'])) {
$ids_to_filter = $input['size_id'];
$query->where(function ($sub) use ($ids_to_filter) {
foreach ($ids_to_filter as $value) {
$sub->where('size_ids', 'like', '%' . $value . '%');
}
});
}
})->get();

If with only ONE is a valid ApplOn use an WHERE OR

 $sub->orWhere('size_ids', 'like', '%' . $value . '%');

Maybe this can help you

The difference between find_in_set() sql and whereIn laravel

If you have comma separated values in your column you should use find_in_set() rather than in. If your column row has following value 'VALIDATED,ACTIVATED' and you will search for ACTIVATED using IN it will fail and return no rows.

return $query
->with('status', 'bank')
->whereHas('status', function (Builder $builder) use ($needle){
return $builder->whereRaw('FIND_IN_SET(?, bank_status)', [$needle]);
});

More about your question - FIND_IN_SET() vs IN()

Laravel whereRaw, orWhereRaw with FIND_IN_SET query not running specifically

As far as I understood,
$request->get('connectivity') has an array which looks like [118, 112].
You wanna filter products which contain any of these two values in their connectivity column.

You just need to create a where closure and run the loop inside it, not outside.

I created a table structure like this:
Sample Table

$connectivity = [550]; # Similar to $request->get('connectivity')
$products = ProductModel::where('active', '1');
$products = $products->where(function($q) use($connectivity){
foreach($connectivity as $key=>$c){
if ($key == 0) {
$q = $q->whereRaw('FIND_IN_SET(' . $c . ',connectivity)');
} else {
$q = $q->orWhere(function ($query) use ($c){
$query->whereRaw('FIND_IN_SET(' . $c . ',connectivity)');
});
}
}
});

I dd()'d the result in the view which gives me 3 for [550].

Sample Image

For [550, 156] I get:

Sample Image



Related Topics



Leave a reply



Submit