You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
user_center/stubs/controller.model.api.stub

137 lines
3.1 KiB

4 years ago
<?php
namespace {{ namespace }};
use {{ namespacedModel }};
use Illuminate\Http\Request;
use {{ namespacedResource }};
use {{ namespacedResourceCollection }};
/**
* @title 基础管理
* @description {{ class }}
* @package {{ namespace }}
* @author zcstatham
*/
class {{ class }} extends BaseController
{
protected $rules = [
'store' => [
'rules' => [
{{ ruleList }}
]
],
'show' => [
'rules' => [
'id' => 'bail|min:1',
],
'custom' => 'exists,{{ namespacedModel }},id'
],
'update' => [
'rules' => [
'id' => 'bail|min:1',
{{ ruleList }}
],
'custom' => [
'exists,{{ namespacedModel }},id',
]
],
'destroy' => [
'rules' => [
'id' => 'bail|min:1',
],
'custom' => [
'exists,{{ namespacedModel }},id',
]
]
];
public function __construct(Request $request)
{
$this->model = new {{ model }}();
parent::__construct($request);
}
/**
* @title 基础列表
* @description 基础列表
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function index()
{
$query = $this->model->query();
if(isset($this->params['name']) && $this->params['name'] != ''){
4 years ago
$query->where('name', $this->params['name']);
}
$data = $query->paginate();
return $this->success(new {{ resourceCollection }}($data));
}
/**
* @title 基础保存
* @description 基础保存
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function store()
{
{{ attrList }}
if($this->model->save()){
return $this->success(new {{ resource }}($this->model));
} else {
return $this->error(500, '基础信息保存失败');
}
}
/**
* @title 基础详情
* @description 基础详情
* @param $id
* @return mixed
* @author zcstatham
*/
public function show($id)
{
return $this->success(new {{ resource }}($this->model->where('id', $id)->first()));
}
/**
* @title 基础更新
* @description 基础更新
* @param $id
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function update($id)
{
$this->model = $this->model->where('id', $id)->first();
{{ attrList }}
if($this->model->save()){
return $this->success(new {{ resource }}($this->model));
} else {
return $this->error(500, '基础信息保存失败');
}
}
/**
* @title 基础删除
* @description 基础删除
* @param $id
* @return \Illuminate\Http\JsonResponse
* @author zcstatham
*/
public function destroy($id)
{
if($this->model->destroy($id)){
return $this->success();
} else {
return $this->error(500, '基础信息保存失败');
}
}
}