ModelSearchWay.php 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. namespace App\Traits;
  3. trait ModelSearchWay
  4. {
  5. private function isSearchLike($str)
  6. {
  7. if (substr($str, 0, 1) == "%" || substr($str, strlen($str) - 1, 1) == "%") {
  8. return true;
  9. }
  10. return false;
  11. }
  12. private function searchWay($query, $param, $column)
  13. {
  14. if ($this->isSearchLike($param)) {
  15. $query->where($column, 'like', $param);
  16. } else {
  17. $query->whereIn($column, array_filter(preg_split('/[,, ]+/is', $param)));
  18. }
  19. return $query;
  20. }
  21. private function sqlSearchWay($sql,$condition,$column)
  22. {
  23. if ($condition){
  24. if (strpos($condition, ',') || strpos($condition, ',') || strpos($condition, ' ')) {
  25. $arr = array_filter(preg_split('/[,, ]+/is', $condition));
  26. $sql .= ' and '.$column.' in (';
  27. foreach ($arr as $index => $arr){
  28. if ($index != 0)$sql .= ',';
  29. $sql .= "'".$arr."'";
  30. }
  31. $sql .= ') ';
  32. unset($condition);
  33. } else {
  34. $sql .= " and $column like '".$condition."' ";
  35. }
  36. }
  37. return $sql;
  38. }
  39. }