方便好用CRUD封装
核心类
trait CrudModel
{
/**
* 构建查询
* @param Builder $q
*/
public function crudSearch(&$q) {}
/*
* 获取列表
* @param array|\Closure $where
* @param array $with
* @param string[] $columns
* @return mixed
*/
public function crudGetList($where = [], $with = [], $columns = ["*"]) {
$q = static::query();
if($where) {
if(is_callable($where)) {
$where($q);
} else if(is_array($where)) {
$q->where($where);
}
}
if($with) {
$q->with($with);
}
$this->crudSearch($q);
return $this->paginate($q, $columns);
}
/*
* 添加或编辑
* @param array $update
* @param array|\Closure $where
* @return static|array
* @throws ParamException
*/
public function crudSave($update, $where = null) {
if(is_string($update)) {
$update = $update::M();
}
if(isset($update[$this->primaryKey])) {
$q = static::query()->where($this->primaryKey, $update[$this->primaryKey]);
if($where) {
if(is_callable($where)) {
$where($q);
} else if(is_array($where)) {
$q->where($where);
}
}
$q = $q->first();
if(!$q) {
throw new QueryException;
}
if($q->fill($update)->save()) {
return $q;
}
return null;
} else {
$m = new static;
if($where) {
$update = array_merge($update, $where);
}
if($m->fill($update)->save()) {
return $m;
}
return null;
}
}
/*
* 获取详情
* @param array|\Closure $where
* @param array $with
* @param string[] $columns
* @return mixed
* @throws ParamException
*/
public function crudGetDetail($where = [], $with = [], $columns = ["*"]) {
$q = static::query();
if($where) {
if(is_callable($where)) {
$where($q);
} else if(is_array($where)) {
$q->where($where);
}
} else {
$q->where($this->primaryKey, Request::input("id"));
}
if($columns) {
$q->select($columns);
}
if($with) {
$q->with($with);
}
$m = $q->first();
if(!$m) {
throw new ParamException;
}
return $m->toArray();
}
/*
* 删除
* @param null|\Closure $callback
*/
public function crudDel($callback = null) {
DB::transaction(function () use($callback) {
$ids = IdValidate::M()["id"];
if($callback) {
$callback($ids);
} else {
static::query()
->whereIn($this->primaryKey, $ids)
->delete();
}
});
}
}
统一模型继承
class Model extends \Illuminate\Database\Eloquent\Model
{
use PaginateFormat, CrudModel;
}
模拟调用
/**
* @public 获取车辆标签列表
* @return JsonResponse
* @throws Throwable
*/
public function getCarTagList() {
$r = new CarTagModel;
$r = $r->crudGetList(function($q) {
$all = Request::all();
foreach ($all as $k => $v) {
if($k === "key") {
$q->where("name", "LIKE", "%{$v}%");
}
}
});
return $this->json($r);
}
/**
* @public 获取车辆标签详情
* @return JsonResponse
* @throws Throwable
*/
public function getCarTagDetail() {
$r = new CarTagModel;
$r = $r->crudGetDetail();
return $this->json($r);
}
/**
* @public 保存车辆标签
* @return JsonResponse
* @throws Throwable
*/
public function setCarTag() {
$r = new CarTagModel;
$r->crudSave(SetCarTagValidate::class);
return $this->json();
}
/**
* @public 删除车辆标签
* @return JsonResponse
* @throws Throwable
*/
public function delCarTag() {
$r = new CarModel;
$r->crudDel();
return $this->json();
}
后记
- 如果觉得方便了开发请把 害怕 打在评论上
本作品采用《CC 协议》,转载必须注明作者和本文链接
推荐文章: