简体   繁体   English

在Laravel查询构建器中结合when()和whereBetween()方法

[英]combining when() and whereBetween() methods in laravel query builder

I have a Model in Larevel that is taking in parameters for reporting total units in the database. 我在Larevel中有一个模型,该模型接受用于报告数据库中总单位的参数。

I want to be able to filter the units returned based on the $entity_ids and the $start and $end dates selected by the user. 我希望能够根据用户选择的$entity_ids$start$end日期过滤返回的单位。

entity_ids is working fine with a simple whereIn() method call, but the dates are causing some issue. 通过一个简单的whereIn()方法调用, entity_ids可以正常工作,但是日期引起了一些问题。

My code in Order.php Model is below: 我在Order.php模型中的代码如下:

public static function getAllOrdersForReporting($entity_ids, $start, $end) {
    $orders = Order::select('all order information entered here')
    ->whereIn('orders.entity_id', $entity_ids)
    ->when($start && $end, function ($query, $start, $end) { //<-- Error Thrown Here
        return $query->whereBetween('order_date', [$start, $end]);
    })
    ->join('entities', 'entities.id', '=', 'ura_orders.entity_id')
    ->join('entity_address_information', 'entity_address_information.entity_id', '=', 'ura_orders.entity_id')->distinct()->get();

    return $orders;
}

In my ReportingController.php I am entering in the following: 在我的ReportingController.php输入以下内容:

public function displayUnits() {
    $entities = request()->entities_ids;
    $start = request()->start_date;
    $end = request()->end_date;
    $orders = Ura_order::getAllOrdersForReporting($entities, $start, $end);

    return view('reporting.pages.units', compact('entities', 'start', 'end', 'orders'));
}

However when I run this, I get the following error: 但是,当我运行此命令时,出现以下错误:

Too few arguments to function App\\Models\\Order::App\\Models{closure}(), 2 passed in C:\\xampp\\htdocs\\mywebsite\\vendor\\laravel\\framework\\src\\Illuminate\\Database\\Concerns\\BuildsQueries.php on line 91 and exactly 3 expected 函数App \\ Models \\ Order :: App \\ Models {closure}()的参数太少,在C:\\ xampp \\ htdocs \\ mywebsite \\ vendor \\ laravel \\ framework \\ src \\ Illuminate \\ Database \\ Concerns \\ BuildsQueries.php中传递了2个参数在第91行,正好是3个

Not exactly sure what this error means, except that the Model is seeing only 2 errors passed in and it expected 3. 不确定该错误的含义,除了Model仅看到2个传入的错误且预期为3个错误。

I marked the line where it is throwing the error up above in the code. 我在上面的代码中标记了将错误引发的行。

Any advice on how to get this to work? 关于如何使它起作用的任何建议? I know the 3rd parameter for when() is supposed to be a callback function, but not sure how to make this work. 我知道when()的第三个参数应该是一个回调函数,但不确定如何使它工作。

You have to use variables in your callback function: 您必须在回调函数中use变量:

->when($start && $end, function ($query) use ($start, $end) {
    return $query->whereBetween('order_date', [$start, $end]);
})

You can try with this code: 您可以尝试使用以下代码:

->when($start && $end, function ($query, $condition) use($start, $end) { 
        return $query->whereBetween('order_date', [$start, $end]);
    })

As already pointed in the comments the tihrd parameter of a when() should be a function , with the use() statement you can pass the variables in the closure. 正如注释中已经指出的, when()的tihrd参数应该是一个functionuse()语句可以在闭包中传递变量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM