当前位置 : 主页 > 网络编程 > PHP >

BaseModel

来源:互联网 收集:自由互联 发布时间:2021-06-28
基类模型封装 whereRaw($where)-select($select); if ($order) { foreach ($order as $key = $val) { $object = $object-orderBy($key, $val); } } return $object; } /** * 获取列表 不分页 * @param string $where 条件 * @param array $sel
基类模型封装
 whereRaw($where)->select($select);
        if ($order) {
            foreach ($order as $key => $val) {
                $object = $object->orderBy($key, $val);
            }
        }
        return $object;
    }

    /**
     * 获取列表  不分页
     * @param string $where 条件
     * @param array $select 查询字段
     * @param int $type 类型 0返回对象格式  1返回数组格式
     * @param array $order 排序
     * @return array|\Illuminate\Support\Collection
     */
    public function get_list($where='1=1',$select=['*'],$type=0,$order=['id'=>'asc'])
    {
        $object = $this->data($where,$select,$order);
        $data = $object->get();
        if ($type == 1){
            return $data->isEmpty() ? [] : $data->toArray();
        }
        return $data;
    }

    /**
     * 获取分页列表
     * @param string $where 条件
     * @param array $select 查询字段
     * @param int $type 类型 0返回对象格式  1返回数组格式
     * @param int $pageNum  每页多少条记录
     * @param array $order
     * @return array|\Illuminate\Contracts\Pagination\LengthAwarePaginator
     */
    public function get_list_page($where='1=1',$select=['*'],$type=0,$pageNum=15,$order=['id'=>'asc'])
    {
        $object = $this->data($where,$select,$order);
        $data = $object->paginate($pageNum);

        if ($type == 1){
            return $data->toArray();
        }
        return $data;
    }

    /**
     * 获取一条数据
     * @param string $where 条件
     * @param array $select 查询字段
     * @param int $type 类型 0返回对象格式  1返回数组格式
     * @return array|mixed
     */
    public function get_one_data($where='1=1',$select=['*'],$type=0)
    {
        $object = $this->data($where,$select);
        $data = $object->first();
        if ($type == 1){
            return $data ? $data->toArray() : [];
        }
        return $data;
    }

    /**
     * 添加数据
     * @param $data
     * @return bool|mixed
     */
    public function add_data($data)
    {
        $result = $this->create($data);
        if ($result) {
            return $result->id;
        }
//        throwException('add error');
        return false;
    }

    /**
     * 修改
     * @param $where
     * @param $data
     * @return bool
     */
    public function edit_data($where,$data){
        $rt = $this->whereRaw($where)->update($data);
        if ($rt){
            return $rt;
        }else{
            return false;
        }
    }

    /**
     * 软删除
     * @param string $where
     * @return bool|null
     */
    public function del_data($where='1=0'){
        return $this->whereRaw($where)->delete();
    }

    /**
     * 硬删除
     * @param string $where
     * @return bool|null
     */
    public function del_data_2($where='0=1')
    {
        return $this->whereRaw($where)->forceDelete();
    }

    /**
     * 查询是否有数据
     * @param $where
     * @return bool
     */
    public function is_have($where)
    {
        return $this->whereRaw($where)->exists();
    }
}
网友评论