分类:PHP记事本 发布时间:2017-09-29 13:57:31 阅读: 作者:郑祥景
<?php namespace App\Repositories; use App\Model\FileCategory; class CategoryRepository { protected $category; public function __construct(FileCategory $category) { $this->category = $category; } /** * 创建记录 * * @param $data * @return mixed */ public function create($data) { return $this->category->create($data); } /** * 获取所有显示记录(带分页) * * @param $page * @param $num * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function get($num) { return $this->category ->orderBy('id', 'desc') ->paginate($num); } /** * 获取所有显示记录(不带分页) * * @param array ...$select * @return mixed */ public function getSimple(...$select) { return $this->category ->select($select) ->orderBy('id', 'desc') ->get(); } /** * 获取显示的搜索结果 * * @param $num * @param $keyword * @return \Illuminate\Contracts\Pagination\LengthAwarePaginator */ public function getSearch($num, $keyword) { return $this->category ->where(function ($query) use ($keyword) { $query->where('categories.name', 'like', "%$keyword%"); }) ->orderBy('id', 'desc') ->paginate($num); } /** * 获取单条记录 * * @param $id * @return mixed */ public function first($id) { return $this->category->find($id); } /** * 获取单条记录(带where和select) * * @param $where * @param array ...$select * @return mixed */ public function selectFirst($where, ...$select) { return $this->category ->select($select) ->where($where) ->first(); } /** * 删除记录 * * @param $id * @return mixed */ public function destroy($id) { return $this->category ->where('id', $id) ->delete(); } /** * 更新记录 * * @param $id * @param $data * @return mixed */ public function update($id, $data) { return $this->category ->where('id', $id) ->update($data); } }
编辑:郑祥景