1, 'name' => '未激活', 'color' => 'gray'], ['id' => 2, 'name' => '激活', 'color' => 'blue'], ['id' => 3, 'name' => '禁用', 'color' => 'red'] ]; /** * 多个Where * @param Object $query * @param array $arr ['status' => 1, 'type' => 2] * @return Object $query */ public function multiwhere($query, $arr) { if ( !is_array($arr) ) { return $query; } foreach ($arr as $key => $value) { $query = $query->where($key, $value); } return $query; } public function getValidator(Request $request, $type) { if($type == 'store') { $validator = Validator::make($request->input('data'), [ 'name' => 'required' ], [ 'name.required' => '名称必填' ]); } else { $validator = Validator::make($request->input('data'), [ 'name' => 'required' ], [ 'name.required' => '名称必填' ]); } return $validator; } public function getOptions() { return $this->where('id', '>', 0)->orderBy('sort')->get()->toArray(); } public function getStatusOptions() { return $this->statusOptions; } /* * type: name|label */ public function getStatus($type = 'name') { $options = $this->statusOptions; $option = $options[0]; foreach($options as $item) { if($item['id'] == $this['status']) { $option = $item; break; } } if($type == 'name') return $option['name']; else return '
' . $option['name'] . '
'; } public function getNameOrLabel($options = [], $type = 'name', $name = 'status') { $option = $options[0]; foreach($options as $item) { if($item['id'] == $this[$name]) { $option = $item; break; } } if($type == 'name') return $option['name']; else return '
' . $option['name'] . '
'; } }