cqp 5 kuukautta sitten
vanhempi
commit
ced000f9d1
1 muutettua tiedostoa jossa 4 lisäystä ja 1 poistoa
  1. 4 1
      app/Service/ProductService.php

+ 4 - 1
app/Service/ProductService.php

@@ -651,7 +651,10 @@ class ProductService extends Service
             ->orderby('id', 'desc');
 
         if(! empty($data['title_t'])) {
-            $model->where('title', 'LIKE', '%'.$data['title_t'].'%')
+            // 清理用户输入,去除前后空白并替换多个连续空格为单个空格
+            $cleanTitle = preg_replace('/\s+/', ' ', trim($data['title_t']));
+            // 构建查询时使用 TRIM 和 REPLACE 来清理数据库字段中的空白字符
+            $model->whereRaw("TRIM(REPLACE(title, ' ', '')) LIKE ?", ['%' . str_replace(' ', '', $cleanTitle) . '%'])
                 ->orWhere('code', 'LIKE', '%'.$data['title_t'].'%')
                 ->orWhere('size', 'LIKE', '%'.$data['title_t'].'%');
         }