From 5aa4055be393f3b761489590c53fe2e331a4f9f2 Mon Sep 17 00:00:00 2001 From: Jaroslav Drzik Date: Thu, 21 Mar 2024 21:11:17 +0100 Subject: [PATCH] Fix complex query --- app/Models/IkeaProducts.php | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/app/Models/IkeaProducts.php b/app/Models/IkeaProducts.php index 7a657d3..2f0bac0 100644 --- a/app/Models/IkeaProducts.php +++ b/app/Models/IkeaProducts.php @@ -41,10 +41,14 @@ class IkeaProducts extends Model if (strlen($text) < 2) return []; $result = $this; - if (strlen($text) >= 2) $result = $result->where("name", 'LIKE', '%'. $text . '%'); - if (strlen($text) > 3) $result->orWhere("typeName", 'LIKE', '%'. $text . '%'); + $result = $this->where(function ($query) use ($text) { + if (strlen($text) >= 2) $query->where("name", 'LIKE', '%'. $text . '%'); + if (strlen($text) > 3) $query->orWhere("typeName", 'LIKE', '%'. $text . '%'); + }); - $result->where('country', $country); + if ($country != '') { + $result->where('country', $country); + } } return $result->get(); }