PlatformContentRepository.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. <?php
  2. namespace App\Repositories;
  3. use App\Models\PlatformContent;
  4. use Symfony\Component\HttpKernel\Exception\HttpException;
  5. /**
  6. * Created by PhpStorm.
  7. * User: durong
  8. * Date: 2019/6/17
  9. * Time: 下午2:06
  10. */
  11. class PlatformContentRepository
  12. {
  13. public function __construct(PlatformContent $platformContent)
  14. {
  15. $this->platformContent = $platformContent;
  16. }
  17. public function index($request)
  18. {
  19. $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
  20. $where = [];
  21. if(isset($request['id'])){
  22. $where[] = ['id', '=', $request['id']];
  23. }
  24. if (isset($request['title'])){
  25. $where[] = ['title', 'like', "%{$request['title']}%"];
  26. }
  27. return $this->platformContent->where($where)->orderBy('updated_at', 'desc')->paginate($perPage);
  28. }
  29. public function create($request)
  30. {
  31. if($this->platformContent->where('title', $request['title'])->exists()){
  32. throw new HttpException(500, '该名称已经存在');
  33. }
  34. $data = [
  35. 'title' => $request['title'],
  36. 'content' => $request['content'],
  37. ];
  38. if (!$this->platformContent->create($data)) {
  39. throw new HttpException(500, '添加失败');
  40. }
  41. }
  42. public function edit($request)
  43. {
  44. $platforms = $this->platformContent->where('id', $request['id'])->first();
  45. $platforms->content = $request['content'];
  46. $platforms->updated_at = date('Y-m-d H:i:s');
  47. $res = $platforms->save();
  48. if (!$res) {
  49. throw new HttpException(500, '平台内容更新失败');
  50. }
  51. }
  52. }