lumen 底层model

最近项目有些紧,对博客懈怠了,临时补充项目中用到的两篇,下面代码是项目中用到的 底层model ,lumen版本6.0X,贴代码:里面有多个数据库链接、分页等等很方便

<?php
/**
 * base model
 *
 * User: Faithlee
 * Date: 2020/5/18
 * Time: 下午3:16
 */

namespace App\Model;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Illuminate\Support\Facades\DB;

class BaseModel extends Model
{
    protected $perPage = 10;

    /**
     * 分页查询方法
     *
     * @param mixed|null $where 查询判断条件
     * @param string $select 查询字段
     * @param array $orders 查询排序
     * @param int $page 查询页码
     * @param int $perPage 自定义分页大小
     * @return mixed
     */
    public function getCommonList($where = null, $select = '*', array $orders = [], int $page, int $perPage = 0)
    {
        $table = $this->getBaseTable()->select($select);
        if ($where) {
            if (!isset($where['bindingParams'])) {
                $table->where($where);
            } else {
                $table->whereRaw($where['where'], $where['bindingParams']);
            }
        }
        if ($orders) {
            foreach ($orders as $key => $value) {
                $table->orderBy($key, $value);
            }
        }
        $pageSize = $perPage ? $perPage : $this->perPage;
        return $table->paginate($pageSize, ['*'], 'page', $page)->toArray();
    }

    public function getCommonFirst($where = [], $select = '*')
    {
        $table = $this->getBaseTable();
        if (is_array($select)) {
            $table->select($select);
        } else {
            $table->selectRaw($select);
        }
        if (!isset($where['bindingParams'])) {
            $table->where($where);
        } else {
            $table->whereRaw($where['where'], $where['bindingParams']);
        }
        return $table->first();

    }

    /**
     * 插入数据
     *
     * @param $data
     * @return mixed
     */
    public function baseInsert($data)
    {
        return $this->getBaseTable()->insertGetId($data);
    }

    /**
     * 更新数据
     *
     * @param array $where
     * @param array $data
     * @return mixed
     */
    public function baseUpdate($where = [], array $data)
    {
        $table = $this->getBaseTable();
        if ($where) {
            $table->where($where);
        }

        return $table->update($data);
    }

    protected function getBaseTable()
    {
        if ($this->connection) {
            return DB::connection($this->connection)->table($this->table);
        }

        return DB::table($this->table);
    }


    public function dealPaginate(QueryBuilder $query, $page)
    {
        $data = $query->paginate($this->perPage, ['*'], 'page', $page)->toArray();

        $newData['list'] = $data['data'];
        $newData['current_page'] = $data['current_page'];
        $newData['per_page'] = $data['per_page'];
        $newData['total'] = $data['total'];
        $newData['total_page'] = ceil($data['total'] / $data['per_page']);

        return $newData;
    }


    /**
     * 查询符合条件的总条数
     * @param array $where
     * @return array
     */
    public function getCommonNum(array $where = [], array $wheres = [])
    {
        $table = $this->getBaseTable();
        if ($where) {
            if (isset($where['where'])) {
                $table->whereRaw($where['where'], $where['bindingParams']);
            } else {
                $table->where($where);
            }
        }

        if ($wheres) {
            foreach ($wheres as $key => $item) {
                $table->whereIn($key, $item);
            }
        }
        $totalNum = $table->count();
        return $totalNum;
    }

    /**
     * 求和
     * @param array $where
     * @return array
     */
    public function getSum(array $where = [],$field = '', array $wheres = [])
    {
        $table = $this->getBaseTable();
        if ($where) {
            if (isset($where['where'])) {
                $table->whereRaw($where['where'], $where['bindingParams']);
            } else {
                $table->where($where);
            }
        }

        if ($wheres) {
            foreach ($wheres as $key => $item) {
                $table->whereIn($key, $item);
            }
        }
        return $table->pluck($field)->sum();
    }

    /**
     * 根据条件查询
     * @param array $where 查询条件
     * @param string $select 查询字段
     * @param array $orders 排序规则 ['id' => 'asc', 'create_time' => 'desc']
     * @param array $likes 模糊查询 ['project_cate_name' => '2323']
     * @return mixed
     */
    public function getBaseInfo($where = [], $select = '*', array $orders = [], array $likes = [])
    {
        $table = $this->getBaseTable();
        $table->select($select);
        if (!empty($where)) {
            if (!isset($where['bindingParams'])) {
                $table->where($where);
            } else {
                $table->whereRaw($where['where'], $where['bindingParams']);
            }
        }

        if (!empty($likes)) {
            foreach ($likes as $lkey => $item) {
                $table->where($lkey, 'like', '%' . $item . '%');
            }
        }

        if (!empty($orders)) {
            foreach ($orders as $key => $value) {
                $table->orderBy($key, $value);
            }
        }

        $data = $table->get()->toArray();
        return $data;
    }

    /**
     * 获取单条记录
     * @param array $where
     * @param string $select
     * @return Model|\Illuminate\Database\Query\Builder|object|null
     */
    public function getInfoRow(array $where = [], $select = '*', array $orders = [])
    {
        $table = $this->getBaseTable();
        $table->select($select);

        if (!empty($where)) {
            if(isset($where['where'])){
                $table->whereRaw($where['where'], $where['bindingParams']);
            }else {
                $table->where($where);
            }
        }
        if ($orders) {
            foreach ($orders as $key => $value) {
                $table->orderBy($key, $value);
            }
        }
        return $table->first();
    }

    /**
     * 查询某个字段的最大值
     * @param array $where
     * @param string $column
     * @return bool|mixed
     */
    public function getMaxValue(array $where = [], string $column = '')
    {
        if (empty($column)) {
            return false;
        }

        $table = $this->getBaseTable();

        if (!empty($where)) {
            $table->where($where);
        }

        return $table->max($column);
    }

    /**
     * whereIn 批量修改
     * @param array $where
     * @param array $update
     * @return int
     */
    public function baseUpdateBatch(array $where = [], $update = [])
    {
        $table = $this->getBaseTable();

        if ($where) {
            foreach ($where as $key => $item) {
                $table->whereIn($key, $item);
            }
        }

        return $table->update($update);
    }

    /**
     * 基本删除
     * @param array $where
     */
    public function baseDelete(array $where = [])
    {
        $table = $this->getBaseTable();
        return $table->where($where)->delete();
    }

    /**
     * 批量插入数据
     *
     * @param $data
     * @return mixed
     */
    public function patInsert($data)
    {
        return $this->getBaseTable()->insert($data);
    }

    /**
     * 改变数据库连接
     * 同一model中需要操作两个数据库中的表
     * @param string $dbName 数据库名称
     * @param string $table 表明
     * @return bool|QueryBuilder
     */
    protected function changeDbTable(string $dbName = '', string $table = '')
    {
        try {
            return DB::connection($dbName ? $dbName : $this->changeConnection)->table($table ? $table : $this->changeTable);
        } catch (\Exception $e) {
            return false;
        }
    }

    /**
     * 批量获取信息
     * @param array $where
     * @param string $select
     * @param array $orders
     * @return array
     */
    public function getInfoBatch(array $wheres, array $where, $select = '*', array $orders = [], $page = 0, $pageSize = 0)
    {
        $table = $this->getBaseTable()->select($select);

        if ($wheres) {
            foreach ($wheres as $key => $item) {
                $table->whereIn($key, $item);
            }
        }

        if ($where) {
            $table->where($where);
        }

        if ($orders) {
            foreach ($orders as $key => $value) {
                $table->orderBy($key, $value);
            }
        }

        if ($page) {
            $pageSize = $pageSize ? $pageSize : $this->perPage;
            return $table->paginate($pageSize, ['*'], 'page', $page)->toArray();
        }

        return $table->get()->toArray();
    }


}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Lumen是一个基于Laravel框架的微型PHP框架,它可以用于构建轻量级的API服务。而Dingo是一个在Laravel框架上构建API的工具包。JWT(JSON Web Token)是一种用于进行身份验证和授权的开放标准。 在使用Lumen结合Dingo和JWT进行开发时,需要先安装Lumen服务提供者、JWT和Dingo的组件。可以使用Composer来管理这些依赖。确保你的电脑上安装了Composer。 在Lumen中,你可以使用控制器来处理请求。引用是一个示例UserController。在这个控制器中,我们注入了JWTAuth实例,并使用它来处理用户的登录请求。其中,我们首先获取请求中的参数,然后使用这些参数进行条件查询。如果登录认证成功,我们会返回一个包含JWT令牌的JSON响应。 对于跨域问题,你可以使用palanik/lumen-cors来解决。引用提供了安装和配置palanik/lumen-cors的方法。你需要通过Composer来安装该组件,并在bootstrap/app.php文件中添加cors路由中间件。 以上就是关于Lumen、Dingo和JWT的一些基本信息和配置方法。如果你有关于它们的更具体的问题,请告诉我。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Lumen 配合 JWT + Dingo 开发流程](https://blog.csdn.net/qq_44149053/article/details/89444892)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] - *3* [lumen+dingo+jwt搭建api系统](https://blog.csdn.net/Chenlevin/article/details/111830096)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v92^chatsearchT0_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值