whereIn('edit_input', CustomFormField::getJsonFieldType()); foreach ($customFormFields as $item) { if ($item->select_item) { continue; } $field = $item->field; $newField = $field . '_details'; if ($item->edit_input == 'files') { $model->$newField = Upload::whereIn('id', (array)$model->$field)->get(); } elseif (isset($item->link_table_name)) { $dynamicModel = tableNameToModel($item->link_table_name); $model->$newField = $dynamicModel->whereIn('id', (array)$model->$field)->get(); } } return $model; } /** * 构建with */ public function buildWith($showRelation = []) { $allWith = $this->model->allRelationFields(); if (!empty($showRelation)) { $allWith = array_intersect($allWith, $showRelation); } $this->model = $this->model->with($allWith); return $this; } /** * 搜索构建 */ public function buildSeacher($conditions) { foreach ($conditions as $condition) { $key = $condition['key'] ?? null; $op = $condition['op'] ?? null; $value = $condition['value'] ?? null; if (!isset($key) || !isset($op) || !isset($value)) { continue; } // 等于 if ($op == 'eq') { $this->model = $this->model->where($key, $value); } // 不等于 if ($op == 'neq') { $this->model = $this->model->where($key, '!=', $value); } // 模糊搜索 if ($op == 'like') { $this->model = $this->model->where($key, 'like', '%' . $value . '%'); } // 否定模糊搜索 if ($op == 'notlike') { $this->model = $this->model->where($key, 'not like', '%' . $value . '%'); } // 否定模糊搜索 if ($op == 'json_contains') { $this->model = $this->model->whereJsonContains($key, (int)$value); } // 范围搜索 if ($op == 'range') { list($from, $to) = explode(',', $value); if (empty($from) || empty($to)) { continue; } $this->model = $this->model->whereBetween($key, [$from, $to]); } } return $this; } /** * 更新关联关系 */ public function updateRelation($all, $model) { $allRelation = $this->model->allRelationFields(true); foreach ($allRelation as $item) { if (!isset($all[$item->link_with_name])) { continue; } foreach ($all[$item->link_with_name] as $v) { if (isset($v['id'])) { $linkModel = $model->{$item->link_with_name}()->find($v['id']); $linkModel->fill($v); $linkModel->save(); } else { // 新增 $fillData = $this->filterRequestColumns($v, $item->link_table_name); $fillData[$item->foreign_key] = $model->id; $model->{$item->link_with_name}()->insert($fillData); } } } return true; } /** * filter request columns by fields * @param $request * @param $linkTableName * @return array */ public function filterRequestColumns($request, $linkTableName) { $columns = (new CustomFormField())->rowTableFieldsByType($linkTableName); $return = []; foreach ($request as $k => $v) { if (!in_array($k, array_keys($columns))) { continue; } if ($k === "password") { if (!$v) { continue; } $v = Hash::make($v); } switch ($columns[$k]) { case "json": $v = json_encode($v, JSON_UNESCAPED_UNICODE); break; default: if (is_array($v)) { if (count($v) == count($v, 1)) { $v = implode(',', $v); } else { $v = json_encode($v, JSON_UNESCAPED_UNICODE); } } } $return[$k] = $v; } return $return; } }