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
Post a Comment