drh2so4/modelscope

这是一个简单的包,允许您使用不同的 Laravel 模型作用域来处理 Laravel 查询构造器。

dev-master 2020-05-02 07:16 UTC

This package is auto-updated.

Last update: 2024-08-29 05:50:15 UTC


README

N|Solid

此包提供有用的查询构造器作用域。此包通过在定义作用域的特质中使用它,为每个模型提供全局作用域。

  • 最新数据检索
  • 周数据检索
  • 年数据检索
  • 月数据检索
  • 日期之间数据检索
  • 不在日期之间数据检索
  • 升序排序
  • 降序排序
  • 多重 WHERE 过滤
  • 多重 orWhere 过滤

安装

运行 Composer Require 命令

$ composer require drh2so4/modelscope

使用命名空间

use drh2so4\ModelScope\ModelScopes;

然后..使用特质

use ModelScopes

整体结构

<?php

namespace App;

use App\Post;
use drh2so4\ModelScope\ModelScopes;

class Post{
    use ModelScopes;
    //
    }
}

包作用域

包作用域旨在使用我们每天使用但尚未指定构建器的各种查询构造器的组合。此包满足了这一需求。

文档

scopeLatestAll()

    public function scopeGetLatestAll($query){
        return $query->orderBy('updated_at','ASC');
    }

用法

$data = User::latestAll()->get();

scopeGetLatest($limit)

    public function scopeGetLatestLimit($query,$limit = 5){
        return $query->orderBy('updated_at','ASC')->take($limit);
    }

用法

$data = User::latestLimit(8)->get();

如果未提供参数,则检索最新 $limit 数量的数据,默认参数值为 5。

scopeTodayData()

      public function scopeTodayData($query){
        $date = Carbon::today();
        $query->whereDate('updated_at',$date);
    }

用法

$data = User::todayData()->get();

检索今天创建的数据。

scopeWeekData()

public function scopeWeekData($query){
        $date = Carbon::today()->subDays(7);
    $query->whereDate('updated_at','>=',$date);
    }

用法

$data = User::weekData()->get();

检索本周创建的数据。

scopeWeekDataLimit($limit)

    public function scopeWeekDataLimit($query,$limit = 5){
        $date = Carbon::today()->subDays(7);
    $query->whereDate('updated_at','>=',$date)->take($limit);
    }

用法

$data = User::weekDataLimit(8)->get();

如果参数为空,则检索本周 $limit 数量的数据,默认参数值为 5。

scopeMonthData()

    public function scopeMonthData($query){
        $date = Carbon::today()->subMonth();
        $query->whereDate('updated_at','>=',$date);
    }

用法

$data = User::monthData()->get();

检索本月创建的数据。

scopeMonthDataLimit($limit)

    public function scopeMonthDataLimit($query,$limit = 5){
        $date = Carbon::today()->subMonth();
        $query->whereDate('updated_at','>=',$date)->take($limit);
    }

用法

$data = User::mothDataLimit(8)->get();

如果参数为空,则检索本月 $limit 数量的数据,默认参数值为 5。

scopeYearData()

    public function scopeYearData($query){
        $date = Carbon::today()->subYear();
        $query->whereDate('updated_at',$date);
    }

用法

$data = User::yearData()->get();

检索本月创建的数据。

scopeYearDataLimit($limit)

    public function scopeYearDataLimit($query,$limit = 5){
        $date = Carbon::today()->subYear();
        return $query->whereDate('updated_at','>=',$date)->take($limit);
    }

用法

$data = User::yearDataLimit(8)->get();

如果参数为空,则检索本年 $limit 数量的数据,默认参数值为 5。

scopeTodayData()

    public function scopeTodayData($query){
        $date = Carbon::today();
        $query->whereDate('updated_at',$date);
    }

用法

$data = User::todayData()->get();

检索今天创建的数据。

scopeTillNowFrom($date)

    public function scopeTillNowFrom($query,$date){
        $this->date_validate($date);
        $from = Carbon::createFromFormat('l jS \\of F Y',trim($date));
        return $query->whereDate('updated_at','>=',$from);
    }

用法

$data = User::tillNowFrom('Thursday 25th of December 1975')->get();

期望参数为 'l jS \of F Y' 格式的日期。检索从 $date 到现在的所有数据。

scopeDataBetween($from,$to)

    public function scopeDataBetween($query,$from_date,$to_date){
        $this->dates_validate($from_date,$to_date);
        $from = Carbon::createFromFormat('l jS \\of F Y',trim($from_date));
        $to = Carbon::createFromFormat('l jS \\of F Y',trim($to_date));
        return $query->whereBetween('updated_at',[$from,$to]);
    }

用法

$data = User::dataBetween('Thursday 25th of December 1975',''Saturday 27th of December 1975')->get();

期望参数为 'l jS \of F Y' 格式的日期 $from 和 $to。检索 $from 和 $to 之间创建的所有数据。

scopeDataNotBetween($from,$to)

    public function scopeDataNotBetween($query,$from_date,$to_date){
        $this->dates_validate($from_date,$to_date);
        $from = Carbon::createFromFormat('l jS \\of F Y',trim($from_date));
        $to = Carbon::createFromFormat('l jS \\of F Y',trim($to_date));
        return $query->whereNotBetween('updated_at',[$from,$to]);
    }

用法

$data = User::dataNotBetween('Thursday 25th of December 1975',''Saturday 27th of December 1975')->get();

期望参数为 'l jS \of F Y' 格式的日期 $from 和 $to。不检索 $from 和 $to 之间创建的数据。

scopeWhereFilter($array)

    public function scopeWhereFilter($query, array $filterData = [])
    {
        foreach ($filterData as $key => $value) {
   

            if (is_null($value) || $value === '') continue;

            $scopeName = ucfirst(str::of($key)->camel());

            if (method_exists($this, 'scope' . $scopeName)) {
                $query->$scopeName($value);
            } else if (is_array($value)) {
                $query->whereIn($key, $value);
            } else {
                $query->where($key, $value);
            }
        }
    }

用法

$data = User::whereFilter(['id' => '2'])->get(); 

期望关联数组,键是列名,值是列值。检索上述用法中提到的所有 WHERE 条件。上述用法等同于 User::where('id',2)->get()

也支持多个 WHERE 条件 !

用法

$data = User::whereFilter(['id' => '2','name' => 'Cat'])->get(); 

检索 id = 2 且 name = cat 的数据

scopeorWhereFilter($array)

    public function scopeorWhereFilter($query, array $filterData = [])
    {
        foreach ($filterData as $key => $value) {
   

            if (is_null($value) || $value === '') continue;

            $scopeName = ucfirst(str::of($key)->camel());

            if (method_exists($this, 'scope' . $scopeName)) {
                $query->$scopeName($value);
            } else if (is_array($value)) {
                $query->orWhere($key, $value);
            } else {
                $query->where($key, $value);
            }
        }
    }

用法

$data = User::orWhereFilter(['id' => '2'])->get(); 

期望关联数组,键是列名,值是列值。检索上述用法中提到的所有 WHERE 条件。上述用法等同于 User::orWhere('id',2)->get()

也支持多个 orWhere 条件 !

用法

$data = User::orWhereFilter(['id' => '2','name' => 'Cat'])->get(); 

检索 id = 2 或 name = cat 的数据

待办事项

异常。更多作用域。一些测试

许可证

MIT

DOCTYPE NEPAL ||DR.H2SO4