nameless
nameless

Reputation: 1521

Eloquent - multiple WHERE LIKE conditions

I'm currently trying to implement a search in my db for keywords. Therefore I split a string seperated by a comma, so I get an array containing a variable number of elements (one keyword).

I know that I can use a eloquent construct like that:

$products = Product::where([['keywords', 'LIKE', %samsung%], [keywords, 'LIKE', 's7']])->paginate(15); 

to find a product with the keywords samsung,galaxy,s7.

Now I need this thing but automatically generated for a variable number of search query parts, so for every keyword in the array I need to add one ['keywords', 'LIKE', '...']...

How can I do this with Laravels Eloquent?

Upvotes: 3

Views: 8127

Answers (4)

Chay22
Chay22

Reputation: 2894

Use closure. First, make sure you stored list of keywords into array or the like. Then ...

$keywords = ['samsung', 's7', 'what else'];

$products = Product::where(function ($query) use ($keywords) {
    foreach ($keywords as $keyword) {
       $query->orWhere('keyword', 'like', $keyword);
    }
})->paginate(15);

other example

$keywords = [
    ['name', 'LIKE', $searchQuery],
    ['category_id', '=', $selectedSubcategory],
];

$products = Product::where(function ($query) use ($keywords) {
    foreach ($keywords as $keyword) {
        $query->where($keyword);
    }
})->paginate(15);

other than other

$keywords = [
    ['name', 'LIKE', $searchQuery],
    ['category_id', '=', $selectedSubcategory],
    ['please_id', '=', $learnPhpArray],
];

$products = Product::query();

foreach ($keywords as $keyword) {
    $products = $products->where($keyword);
}

return $products->paginate(15);

What does the orWhere do? Does it connect the query parts with a AND?

No, with OR. As the inverse(?) the where itself does AND by default.


References

Upvotes: 9

laravel 5.6

    $keyWords = KeyWordModel::all();
    $keyWordQuerys = [];
    foreach ($keyWords as $key => $item) {
        $keyWordQuerys[$key] = ['title', 'like', '%'.$item->name.'%'];
    }
    $contents = ContentModel::query();
    foreach ($keyWordQuerys as $key => $item) {
        $contents = $contents->orwhere([$item]);
    }
    $contents = $contents->orderBy('pubDate', 'DESC')->get();

Upvotes: 0

apokryfos
apokryfos

Reputation: 40730

You can do this:

$query = "Samsung galaxy S7"; // Or whatever the query is

$words = preg_split("/[ ,.]/",$query); //Split on space comma or dot. Maybe need more?

$queries =  array_map(function ($word) { 
       return [ "keywords", "LIKE", "%$word%" ]; 
 }, $words); //Transform single words to array of queries

$products = Product::where($queries)->paginate(15); 

Check how the first part of the code works at: https://eval.in/730772

Upvotes: 0

Ulrik McArdle
Ulrik McArdle

Reputation: 664

Isn't it just to generate the array? Or am I misunderstanding the question?

<?php

$keys = ['samsung', 'lg', 'sony', 'nokia', 'apple'];
$keywords = [];
foreach($keys as $key){
    $keywords[] = ['keywords', 'LIKE', '%'.$key.'%'];
}

$products = Product::where($keywords)->paginate(15);

Upvotes: 4

Related Questions