| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- <?php
- namespace App\Traits;
- trait ModelSearchWay
- {
- private function isSearchLike($str)
- {
- if (substr($str, 0, 1) == "%" || substr($str, strlen($str) - 1, 1) == "%") {
- return true;
- }
- return false;
- }
- private function searchWay($query, $param, $column)
- {
- if ($this->isSearchLike($param)) {
- $query->where($column, 'like', $param);
- } else {
- mb_regex_encoding('utf-8');
- $query->whereIn($column, array_filter(preg_split('/[,, ]+/u', $param)));
- }
- return $query;
- }
- private function sqlSearchWay($sql,$condition,$column)
- {
- if ($condition){
- if (strpos($condition, ',') || strpos($condition, ',') || strpos($condition, ' ')) {
- $arr = array_filter(preg_split('/[,, ]+/is', $condition));
- $sql .= ' and '.$column.' in (';
- foreach ($arr as $index => $arr){
- if ($index != 0)$sql .= ',';
- $sql .= "'".$arr."'";
- }
- $sql .= ') ';
- unset($condition);
- } else {
- $sql .= " and $column like '".$condition."' ";
- }
- }
- return $sql;
- }
- }
|