laravel5.5版本数据查询 基于  prettus/l5-repository 插件

首先安装好 prettus/l5-repository插件  配置相关参数

1 根据条件查询数据总数

public function getTotal(array $where)
{
    return $this->model->where($where)->count();
}

2 新增一条记录

public function addData(array $data)
{
    return $this->model->insertGetId($data);
}

3 根据条件查询一条记录

public function getRow(int $id)
{
    $rs = $this->model->where('id',$id)->first();
    return $rs ? $rs->toArray() : [];
}

4 根据条件删除一条记录

public function deleteData(array $where)
{
    return $this->model->where($where)->delete();
}

5 根据条件查询指定字段的值

public function getField(array $where,string $field_name)
{
    return $this->model->where($where)->value($field_name);
}

6 根据条件查询数据分页查询
public function getDataList(array $where, int $page = 1, int $page_size = 20) { $skip = ($page-1)*$page_size; $rs = $this->model ->select('*') ->orderBy("id", "desc") ->where($where) ->offset($skip) ->limit($page_size) ->get(); return $rs ? $rs->toArray() : []; } 7 left join 查询
public function getLeftJoinData(int $page_size=10){ $rs = $this->model ->leftjoin('b','b.id','=','a.b_id') ->select(['a.id','a.b_id','a.name','b.title']) ->where('b.status',1) ->where('a.status',1) ->orderBy("a.id", "desc") ->take($page_size) ->get(); return $rs ? $rs->toArray() : []; }

 

posted on 2022-11-17 11:52  ヽ坚强的蘑菇  阅读(68)  评论(0编辑  收藏  举报