You can do something like the following:
$term = $request->get('term'); $keywords = explode(" ", $term); $article = Article::query(); foreach($keywords as $word){ $article->orWhere('title', 'LIKE', '%'.$word.'%'); } $articles = $article->published()->get();
If you only want results that contain all the words in the query, just replace orWhere with where .
If you want to filter out specific words, you can add something like:
$filtered = ["a", "an", "the"]; $filteredKeywords = array_diff($keywords, $filtered);
Alternatively, you can pass closure if you want more dynamically:
$filteredKeywords = array_filter($keywords, function($word) { return strlen($word) > 2; });
source share