分类:PHP记事本 发布时间:2017-09-29 14:02:37 阅读: 作者:郑祥景
<?php namespace App\Services\Manage; use App\Repositories\FileCategoryRepository; use Exception; class FileCategoryServer { protected $category; public function __construct(FileCategoryRepository $category) { $this->category = $category; } /** * 通过id验证记录是否存在以及是否有操作权限 * 通过:返回该记录 * 否则:抛错 * * @param $id * @return \Illuminate\Database\Eloquent\Collection|\Illuminate\Database\Eloquent\Model|null|static|static[] */ public function validata($id) { $first = $this->category->first($id); throw_if(empty($first), Exception::class, '未找到该记录!', 404); throw_if(can('control'), Exception::class, '没有权限!', 403); return $first; } /** * 获取需要的记录(有分页) * * @param int $num * @param null $keyword * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function get($num = 10000, $keyword = null) { if (!empty($keyword)) { return $this->category->getSearch($num, $keyword); } return $this->category->get($num); } /** * 获取需要的记录(无分页) * * @param array ...$select * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function getSimple(...$select) { return $this->category->getSimple(...$select); } /** * 查找指定id的记录 * * @param $id * @return mixed */ public function first($id) { return $this->validata($id); } /** * 更新或编辑 * * @param $post * @param null $id * @return mixed */ public function updateOrCreate($post, $id = null) { //打包数据 $data['name'] = $post['name']; $data['parent_id'] = $post['parent_id']; //执行插入或更新 return empty($id) ? $this->category->create($data) : $this->category->update($id, $data); } /** * 删除 * * @param $id * @return bool|null */ public function destroy($id) { //验证是否可以操作当前记录 $this->validata($id)->toArray(); //执行删除 return $this->category->destroy($id); } }
编辑:郑祥景