1

I have some conditions in array like

    $category = Input::get('category');
    $cuisine = Input::get('cuisine');
    $veg = Input::get('veg');
    $trending = Input::get('trending');
    $time = Input::get('time');

    if($category) $conditions['category'] = $category;
    if($cuisine) $conditions['cuisine'] = $cuisine;
    if($veg) $conditions['veg'] = $veg;
    if($trending) $conditions['trending'] = $trending;

How can I make

$list  = Data::where($conditions)->where('cuisine','LIKE','%'.$cuisine.'%')->get();

Is it possible to enter LIKE % in this statement

if($cuisine) $conditions['cuisine'] = $cuisine;

The problem is that if I want to add this where('cuisine','LIKE','%'.$cuisine.'%') several areas it needs to be updated. and in some cases, if cuisine is not present everything cannot be fetched

I want to perform LIKE statement for only cuisine data.

4
  • What do you want actually ? some search like function ? can you explain please it's difficult to understand Commented Dec 20, 2017 at 7:13
  • 1
    Is it possible to do where('cuisine','LIKE','%'.$cuisine.'%') in this statement if($cuisine) $conditions['cuisine'] = $cuisine; Commented Dec 20, 2017 at 7:15
  • I'm sorry I still do not understand what you are saying Commented Dec 20, 2017 at 7:19
  • I want to perform this Data::where($conditions)->get(); and i don't want to add where('cuisine','LIKE','%'.$cuisine.'%') this in. so is it possible to add the LIKE %% on the $conditions['cuisine'] variable? Commented Dec 20, 2017 at 7:21

5 Answers 5

5

Sure, you can do that by creating an array with this format:

[['column1', 'like', '%' . $filter1 . '%'], ['column2', 'like', '%' . $filter2 . '%']]

For example:

$fields = ['category', 'cuisine', 'veg', 'trending', 'time'];

foreach ($fields as $field) {
    if ($request->get($field)) {
        $conditions[] = [$field, 'like', '%' . $request->get($field) . '%'];
    }
}

$list = Data::where($conditions)->get();

Another example from the docs:

You may also pass an array of conditions to the where function:

$users = DB::table('users')->where([
    ['status', '=', '1'],
    ['subscribed', '<>', '1'],
])->get();

https://laravel.com/docs/5.5/queries#where-clauses

Update

You've just updated your question and said you want to use like only for $cuisine. In this case, you can use a closure:

->where(function($q) use($request) {
    if ($request->cuisine) {
        $q->where('cuisine', 'like', '%' . $request->cuisine . '%');
    }
})

Or you could use when():

->when($request->cuisine, function ($q) use ($cuisine) {
    return $q->where('cuisine', 'like', '%' . $request->cuisine . '%');
})
Sign up to request clarification or add additional context in comments.

Comments

1

Well, you can do it in parts:

$query = Data::where($conditions);
if($cuisine) {
    $query->where('cuisine','LIKE','%'.$cuisine.'%');
}
$list = $query->get();

Comments

0

You can do it like,

$query = DB::table('data');

$category = Input::get('category');
$cuisine = Input::get('cuisine');
$veg = Input::get('veg');
$trending = Input::get('trending');
$time = Input::get('time');

if($category) {
    $query->where('category','LIKE','%'.$category.'%');
}
if($cuisine) {
    $query->where('cuisine','LIKE','%'.$cuisine.'%');
}
if($veg) {
    $query->where('veg','LIKE','%'.$veg.'%');
}
if($trending) {
    $query->where('trending','LIKE','%'.$trending.'%');
}
if($time) {
    $query->where('time','LIKE','%'.$time.'%');
}

$list = $query->get();

I hope you will understand.

Comments

0

Why not just assign as blank value as default as it will pass in LIKE for all cases

$conditions['cuisine']= (isset($cuisine)&&$cuisine)) ? $cuisine : '';

Comments

0

Well, you have to assign query to some variable:

    $query = Data::where($conditions);
     if($cuisine) { 
    $query = $query->where('cuisine','LIKE','%'.$cuisine.'%'); 
    } 
$list = $query->get();

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.