CmsContentTemplateRepository.php 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\CmsContentTemplate;
  4. use Symfony\Component\HttpKernel\Exception\HttpException;
  5. class CmsContentTemplateRepository {
  6. public function __construct(CmsContentTemplate $cmsContentTemplate) {
  7. $this->cmsContentTemplate = $cmsContentTemplate;
  8. }
  9. //模板列表
  10. public function index($request)
  11. {
  12. $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
  13. $where = [];
  14. if(isset($request['id'])){
  15. $where[] = ['id', '=', $request['id']];
  16. }
  17. if (isset($request['is_open'])){
  18. $where[] = ['is_open',$request['is_open']];
  19. }
  20. if (isset($request['city_id'])){
  21. $where[] = ['city_id',$request['city_id']];
  22. }
  23. return $this->cmsContentTemplate->where($where)->orderBy('id', 'asc')->paginate($perPage);
  24. }
  25. /**
  26. * 新建模板
  27. */
  28. public function create($request)
  29. {
  30. if($this->cmsContentTemplate->where('title', trim($request['title']))->exists()){
  31. throw new HttpException(500, '该模块已经存在');
  32. }
  33. $subject = [
  34. 'title' => $request['title'],
  35. 'city_id' => $request['city_id'],
  36. 'city_name' => $request['city_name'],
  37. 'apply_type' => $request['apply_type'],
  38. 'is_open' => $request['is_open'],
  39. ];
  40. if (!$this->cmsContentTemplate->create($subject)) {
  41. throw new HttpException(500, '添加失败');
  42. }
  43. }
  44. }