123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263 |
- <?php
- namespace App\Repositories;
- use App\Models\PlatformContent;
- use Symfony\Component\HttpKernel\Exception\HttpException;
- /**
- * Created by PhpStorm.
- * User: durong
- * Date: 2019/6/17
- * Time: 下午2:06
- */
- class PlatformContentRepository
- {
- public function __construct(PlatformContent $platformContent)
- {
- $this->platformContent = $platformContent;
- }
- public function index($request)
- {
- $perPage = isset($request['per_page']) ? $request['per_page'] : env('PER_PAGE');
- $where = [];
- if(isset($request['id'])){
- $where[] = ['id', '=', $request['id']];
- }
- if (isset($request['title'])){
- $where[] = ['title', 'like', "%{$request['title']}%"];
- }
- return $this->platformContent->where($where)->orderBy('updated_at', 'desc')->paginate($perPage);
- }
- public function create($request)
- {
- if($this->platformContent->where('title', $request['title'])->exists()){
- throw new HttpException(500, '该名称已经存在');
- }
- $data = [
- 'title' => $request['title'],
- 'content' => $request['content'],
- ];
- if (!$this->platformContent->create($data)) {
- throw new HttpException(500, '添加失败');
- }
- }
- public function edit($request)
- {
- $platforms = $this->platformContent->where('id', $request['id'])->first();
- $platforms->content = $request['content'];
- $platforms->updated_at = date('Y-m-d H:i:s');
- $res = $platforms->save();
- if (!$res) {
- throw new HttpException(500, '平台内容更新失败');
- }
- }
- }
|