【问题标题】:Get data whereBeetween two coloms获取两列之间的数据
【发布时间】:2018-03-27 09:07:16
【问题描述】:

我正在使用 Laravel 5.5。
我有表 centrals 和模型 Centrals 和这样的桌子

我的控制器

    $from = $request->year_from;
    $to   = $request->year_to;
    $month_from = $request->month_from;
    $month_to   = $request->month_to;
    $param = $request->get('parameters_id', []);

    $search = Centrals::whereIn('parameters_id', $param)
    ->where('type', 'Monthly')
    ->where('year', $from)
    ->whereBetween('months_id', [$month_from, $month_to])
    ->orderBy('year')
    ->get();

现在我如何获取数据 示例请求:

$request->year_from = 2016;
$request->month_from = 1; /* 1 =Jan*/
$request->year_from = 2018;
$request->month_from = 3; /* 3 =Mar*/

【问题讨论】:

    标签: php laravel laravel-5 controller eloquent


    【解决方案1】:

    以下应该可以解决问题:

    $search = Centrals::whereIn('parameters_id', $param)
            ->where('type', 'Monthly')
            ->where(function($q) use ($yearFrom, $monthFrom) {
                $q->where('year', '>', $yearFrom);
                $q->orWhere(function($q2) use ($yearFrom, $monthFrom) {
                    $q2->where('year', $yearFrom);
                    $q2->where('month', '>=', $monthFrom);
                });
            })
            ->where(function($q) use ($yearTo, $monthTo) {
                $q->where('year', '<', $yearTo);
                $q->orWhere(function($q2) use ($yearTo, $monthTo) {
                    $q2->where('year', $yearTo);
                    $q2->where('month', '<=', $monthTo);
                });
            })->get();
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-19
      • 1970-01-01
      • 1970-01-01
      • 2020-06-18
      • 2016-04-17
      • 1970-01-01
      相关资源
      最近更新 更多