How can I do a Like-query, with multiple values to search for?
$searchWords = explode(' ', Input::get('search'));
Then I get an array of words that were given for the search.
How can I pass it in this:
$pages = Page::where('content', 'LIKE', '%'.$singleWord.'%')->distinct()->get();
A loop can't work, then it overwrites the $pages always; then it keeps always the latest search:
foreach($searchWords as $word){
$pages = Page::where('content', 'LIKE', '%'.$word.'%')->distinct()->get();
}
A loop is the solution but you only need to add the where condition without executing the query
$pages = Page::query();
foreach($searchWords as $word){
$pages->orWhere('content', 'LIKE', '%'.$word.'%');
}
$pages = $pages->distinct()->get();
i will code it for you :
$pages = Page->where(function($query) use($word){
foreach($searchWords as $word){
$query->orWhere('content', 'LIKE', '%'.$word.'%');
}
})->get();
you can try that , i hope that will help :)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With