【问题标题】:how to use whereNull and orwhereBetween in Laravel at the same time?如何在 Laravel 中同时使用 whereNull 和 orwhereBetween?
【发布时间】:2021-08-31 00:50:12
【问题描述】:

我正在构建一个系统,用户输入日期范围并从数据库中以表格的形式获取数据。我在数据库 payment_status、value_date、pmt_msg_date、acceptance_date 中有列。现在我想通过用户提供的日期范围从数据库过滤器中检索所有数据,其中 payment_status 为空。我怎样才能得到它?

这是我的控制器

function payment_list_table(Request $req) {
    if (session()->has('user')) {
        $data = Doc::whereNull('payment_status')
            ->orwhereBetween('value_date', [$req->from, $req->to])
            ->orwhereBetween('pmt_msg_date', [$req->from, $req->to])
            ->orwhereBetween('acceptance_date', [$req->from, $req->to])
            ->get();
        return view('payment/table_payment_list', ['datas'=>$data,]);
    } else {

    }
}

我能够获取该日期范围内的所有数据以及付款状态不为空的数据。我该如何解决它。

【问题讨论】:

  • 您的意思是您只想获取付款状态为null??的那些行?
  • $data = Doc::whereNull('payment_status')->where(function ($query)use($req){ $query->whereBetween('value_date',[$req-> from,$req->to]) $query->orwhereBetween('pmt_msg_date',[$req->from,$req->to])->orwhereBetween('acceptance_date',[$req->from,$req ->to]); })->get();这是你在找吗
  • 是....以及用户通过$req接收到的日期范围......
  • 那么你必须指定数据类型 pmt_msg_date 或acceptance_date 以及格式 $req->from,$req->to

标签: laravel laravel-8


【解决方案1】:

您可以为此使用带有闭包的高级 where 子句。

$data = Doc::whereNull('payment_status')
        ->where(function ($query) use ($req) {
            $query->whereBetween('value_date', [$req->from, $req->to])
                ->orWhereBetween('pmt_msg_date', [$req->from, $req->to])
                ->orWhereBetween('acceptance_date', [$req->from, $req->to]);
        })
        ->get();

【讨论】:

    猜你喜欢
    • 2019-07-06
    • 2018-09-27
    • 2016-11-04
    • 2021-02-07
    • 1970-01-01
    • 2018-07-06
    • 1970-01-01
    • 1970-01-01
    • 2022-08-04
    相关资源
    最近更新 更多