menu

Questions & Answers

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?

Comments:
2023-01-07 20:30:49
why dont you just loop over your search query parts and add them to an array then use that array in the where method
Answers(4) :

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

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);
Comments:
2023-01-07 20:30:49
The closure way (like Chay22 says) is probably the way to go, since it is the cleanest and most "Laravel" :)
2023-01-07 20:30:49
I'd like to ask you the same question as on the closure thing: How could I combine this with other selection things, like I mentioned on the other question?
2023-01-07 20:30:49
You could always chain another where (...)->where (...)->paginate (15);

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

Comments:
2023-01-07 20:30:49
What does the orWhere do? Does it connect the query parts with a AND?
2023-01-07 20:30:49
Also, I need to pass some more selection things in, so it's something like that $products = Product::where([['name', 'LIKE', $searchQuery], ['category_id', '=', $selectedSubcategory]])->paginate(15); and then I need the keyword thing... How can I combine this then?

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();