Code Monkey home page Code Monkey logo

laravel-orm-support's Introduction

Laravel ORM Support

扩展 Laravel ORM

添加了按月分表的支持

Installing

$ composer require luffluo/laravel-orm-support:~1.0 -vvv

Usage

Notice

复制 Illuminate\Database\Query\Builder 类到根目录下 replaces\Database\Query\Builder.php 然后用下面的 getBindings 方法覆盖原来的

<?php

declare(strict_types = 1);

namespace Illuminate\Database\Query;

class Builder
{
    /**
    * Get the current query value bindings in a flattened array.
    *
    * @return array
    */
    public function getBindings()
    {
        $bindings = Arr::flatten($this->bindings);
        
        if ($this->unions && count($this->bindings['union']) <= 0) {
            foreach ($this->unions as $union) {
                $bindings = array_merge($bindings, $union['query']->getBindings());
            }
        }
        
        return $bindings;
    }
}

并在 composer.json 里添加如下配置, 然后执行 composer dump

{
    "autoload": {
        "files": [
            "replaces/Database/Query/Builder.php",
        ]
    }
}

表名要使用如下格式 xxxx_202111, xxxx_202112 等

按月分表的使用

use \Luffluo\LaravelOrmSupport\Traits\MonthlyScale;

class Model
{
    use MonthlyScale;
}

添加上面的 Traitmodel 后,就像原来使用 model 一样,会查询当月的数据

查询当前月

Model::query()->count();

查询上周的数据

// 上周
Model::queryForLastWeek()->count();

// 上上周
Model::queryForLastWeeks(2)->count();

查询某个时间段的数据, 时间支持 Carbon

Model::queryForPeriod('2019-01', '2019-11')->count();

查询某年某月的数据,时间支持 Carbon

Model::queryForYearMonth('201901')->count();

select

Model::queryForPeriod('2019-11-26', '2020-11-26')
    ->unionSelect('xxx', 'xxx')
    ->unionSelectRaw('xxx', 'xxx')
    ->count();

where

Model::queryForPeriod('2019-11-26', '2020-11-26')
    ->unionWhere('xxx', 'xxx')
    ->unionOrWhere('xxx', 'xxx')
    ->unionWhereIn('xxx', ['xx', 'xx'])
    ->unionWhereBetween('xx', ['xxx', 'xxx'])
    ->count();

groupBy

Model::queryForPeriod('2019-11-26', '2020-11-26')
    ->unionGroupBy('xxx', 'xxx')
    ->count();

License

MIT

laravel-orm-support's People

Contributors

luffluo avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.