[ 'rules' => [ 'title' => 'bail|required|unique:App\Models\CompanyT|max:20', 'name' => 'bail|required|unique:App\Models\CompanyT|max:20', 'pid' => 'required', ] ], 'show' => [ 'rules' => [ 'id' => 'bail|inter|min:1', ], 'custom' => 'exists,App\Models\CompanyT,id' ], 'update' => [ 'rules' => [ 'id' => 'bail|inter|min:1', 'title' => 'required|max:20', 'name' => 'bail|required|max:20', 'pid' => 'required', ], 'custom' => [ 'exists,App\Models\CompanyT,id', 'unique,App\Models\CompanyT,title,id', 'unique,App\Models\CompanyT,name,id' ] ], 'destroy' => [ 'rules' => [ 'id' => 'bail|inter|min:1', ], 'custom' => [ 'exists,App\Models\CompanyT,id', ] ] ]; public function __construct(Request $request) { $this->model = new CompanyT(); parent::__construct($request); } /** * @title 公司列表 * @description 公司列表 * @return \Illuminate\Http\JsonResponse * @author zcstatham * @time 2021/1/28 */ public function index() { $query = CompanyT::query(); if(isset($this->params['name']) && $this->params['name'] != ''){ $query->where('name', $this->params['name']); } if(isset($this->params['title']) && $this->params['title'] != ''){ $query->where('title', 'like', '%'. $this->params['title'] .'%'); } $data = $query->paginate(); return $this->success(new CompanyCollection($data)); } /** * @title 公司新增 * @description 公司新增 * @return \Illuminate\Http\JsonResponse * @author zcstatham * @time 2021/1/28 */ public function store() { $company = new CompanyT(); $company->title = $this->params['title']; $company->name = $this->params['name']; $company->pid = $this->params['pid']; if($company->save()){ return $this->success(new Company($company)); } else { return $this->error(500, '配置信息保存失败'); } } /** * @title 公司详情 * @description 公司详情 * @param $id * @return mixed * @author zcstatham * @time 2021/1/28 */ public function show($id) { return $this->success(new Company($this->model->where('id', $id)->first())); } /** * @title 公司更新 * @description 公司更新 * @param $id * @return \Illuminate\Http\JsonResponse * @author zcstatham * @time 2021/1/28 */ public function update($id) { $company = $this->model->where('id', $id)->first(); $company->title = $this->params['title']; $company->name = $this->params['name']; $company->pid = $this->params['pid']; if($company->save()){ return $this->success(new Company($company)); } else { return $this->error(500, '配置信息保存失败'); } } /** * @title 公司删除 * @description 公司删除 * @param $id * @return \Illuminate\Http\JsonResponse * @author zcstatham * @time 2021/1/28 */ public function destroy($id) { if(CompanyT::destroy($id)){ return $this->success(); } else { return $this->error(500, '配置信息保存失败'); } } }