| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135 | <?phpnamespace App\Repositories;use App\Models\Article;use Prettus\Repository\Eloquent\BaseRepository;/** * Class ArticleRepositoryEloquent. * * @package namespace App\Repositories; */class ArticleRepository extends BaseRepository{    /**     * Specify Model class name     *     * @return string     */    public function model()    {        return Article::class;    }    /**     * @param $where array     * @return null|\Illuminate\Database\Eloquent\Model     */    public function getArticles($where, $page)    {        /*$res = $this->model->when(get_subsite_id()>0, function ($query) {                $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});        })->where($where)->orderByRaw('list_order asc,id desc');*/        $res = $this->model->whereHas('subsites', function ($query) {            $query->where('subsite_id', get_subsite_id());        })->where($where)->orderByRaw('list_order desc,created_at desc');        if ($page) {            return $res->paginate($page);        } else {            return $res->get();        }    }    /*     *根据分类查询     */    public function getArticle($where,$page)    {        return $this->model->whereHas('show_category',function ($query) use ($where){            $query->where($where);        })->paginate($page);    }    /*     *根据分类查询     */    public function getArticlesByType($type_id,$limit)    {        $where = array('is_display'=>'1','type_id'=>$type_id);        return  $this->model->selectRaw('id,small_img')->whereHas('subsites', function ($query) {            $query->where('subsite_id', get_subsite_id());        })->where($where)->where('small_img','<>','')->orderByRaw('list_order desc,created_at desc')->limit($limit)->get();    }    /**     * @param $where array     * @return null|\Illuminate\Database\Eloquent\Model     */    public function getAllArticles($where, $orWhere, $page)    {        /*$res = $this->model->when(get_subsite_id()>0, function ($query) {                        $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});                    })->where(function ($query) use ($where, $orWhere) {                        $query->where($where)->orwhere($orWhere);                })->orderByRaw('list_order asc,id desc');*/        $res = $this->model->whereHas('subsites', function ($query) {            $query->where('subsite_id', get_subsite_id());        })->where(function ($query) use ($where, $orWhere) {            $query->where($where)->orwhere($orWhere);        })->orderByRaw('list_order desc,created_at desc');        if ($page) {            return $res->paginate($page);        } else {            return $res->get();        }    }    public function getPropertyArticles($property_id, $limit)    {        $where = array('is_display'=>'1','property_id'=>$property_id);        /*return  $this->model->when(get_subsite_id()>0, function ($query) {            $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});        })->where($where)->orderByRaw('list_order asc,id desc')->limit($limit)->get();*/        return  $this->model->whereHas('subsites', function ($query) {            $query->where('subsite_id', get_subsite_id());        })->where($where)->orderByRaw('list_order desc,created_at desc')->limit($limit)->get();    }    public function firstWhere($where)    {        return $this->model->where($where)->first();    }    public function incrementData($where, $num, $filed)    {        return $this->model->where($where)->increment($filed, $num);    }    public function filterArticles($where = array(), $whereIn = array(), $order = array())    {        /*$rst = $this->model->where($where);*/        /*$rst = $this->model->when(get_subsite_id()>0, function ($query) {                    $query->whereHas('subsites', function ($query) { $query->where('subsite_id', get_subsite_id());});                })->where($where);*/        $rst = $this->model->whereHas('subsites', function ($query) {            $query->where('subsite_id', get_subsite_id());        })->where($where);        if ($whereIn) {            foreach ($whereIn as $k => $v) {                $rst->whereIn($k, $v);            }        }        if ($order) {            if (is_array($order)) {                foreach ($order as $k => $v) {                    $rst->orderBy($k, $v);                }            } else {                $rst->orderbyRaw($order);            }        }        return $rst->get();    }}
 |