Laravel search logic issue -


i developing search module in laravel 5.1 searches businesses along phone numbers (records).

-- tables businesses: id, name, status records: id, business_id, number, note 

the search must return results when both business name , records note found given keywords. returns businesses if associated records(note) not match.

here code:-

$keywords = explode(" ", request::get('keywords')); $businesses = app\business;  $businesses = $businesses->where(function ($query) use ($keywords) {     foreach ($keywords $name) {         $query->orwhere('name', 'like', "$name%");     } });  $businesses = $businesses->with(['records' => function ($query) use ($keywords) {     $query->where('note', 'like', '%'.$keywords[0].'%');      foreach ($keywords $note) {         $query->orwhere('note', 'like', "%$note%");     } }]);  $businesses = $businesses->where('status', 1); $businesses = $businesses->paginate($limit);  return $businesses; 

don't explode keywords because separated space mentioned. try following.

$businesses = $businesses->with(['records' => function ($query) use ($keywords) {             $query->where('note', 'like', '%'.$keywords.'%');         }])->where('name', 'like', '%'.$keywords.'%')->get(); 

Comments

Popular posts from this blog

Ansible - ERROR! the field 'hosts' is required but was not set -

customize file_field button ruby on rails -

SoapUI on windows 10 - high DPI/4K scaling issue -