/** * @param $keywords string * @param $field string * @return array|string */ public function advance_where_or($field,$keywords) { $keywords = trim($keywords); $keywords = preg_replace('/s+/', ' ', $keywords); $where = []; if (strlen($keywords) > 0) { $keywordsArr = explode(" ", $keywords); if (count($keywordsArr) > 0) { $sql = ""; $arrLen = count($keywordsArr); foreach ($keywordsArr as $k => $v) { if ($k < $arrLen - 1) { $sql .= " `" . $field . "` LIKE '%" . $v . "%' OR "; }else{ $sql .= " `" . $field . "` LIKE '%" . $v . "%'"; } } $where = $sql; } } return $where; } User::where(advance_where_or("nickname","张三 李四"))->select();