简体   繁体   English

Laravel 查询生成器何时/如果

[英]Laravel Query Builder When / If

I'm trying to filter out some specific records from a table based on user filter but running into issue on how to properly format it.我正在尝试根据用户过滤器从表中过滤掉一些特定记录,但遇到了如何正确格式化它的问题。 I have sales_channel and total columns from Orders table.我有来自Orders表的sales_channeltotal列。

If sales_channel == Non-Amazon then I need to grab all of those records regardless of total .如果sales_channel == Non-Amazon那么我需要获取所有这些记录而不管total Alternatively, if sales_channel !== Non-Amazon then I only want to grab records that have a total > 0或者,如果sales_channel !== Non-Amazon那么我只想获取total > 0

For my initial query I have the following which works as expected:对于我的初始查询,我有以下按预期工作的内容:

$orders = Orders::where('sales_channel', '=', 'Non-Amazon')->orWhere(function ($query) {
    $query->where('total', '>', 0);
})->get();

However, when a user sends a request to filter data, I'm not sure how to properly filter the request.但是,当用户发送过滤数据的请求时,我不确定如何正确过滤请求。 This is what I currently have where $request['sales_channel'] is an array of sales channels that they want to return:这是我目前拥有的$request['sales_channel']是他们想要返回的销售渠道数组:

$request = $request->data;
$orders = Orders::query();

if ($request['sales_channel']) {
    $orders->whereIn('sales_channel', $request['sales_channel']);
}

How would I make it so if $request['sales_channel'] contains Non-Amazon , grab all of the Non-Amazon records, but for any other sales channel, total > 0 must be true.如果$request['sales_channel']包含Non-Amazon ,我将如何做到这一点,获取所有非亚马逊记录,但对于任何其他销售渠道, total > 0必须为真。


Example:例子:

sales_channel | total
--------------+----------------
Non-Amazon    | 19.99
Non-Amazon    | 0.00
Amazon.com    | 11.00
Amazon.com    | 0.00
Amazon.ca     | 22.00
Amazon.com.mx | 19.99

User wants to filter by Non-Amazon and Amazon.com results:用户希望按非亚马逊和 Amazon.com 结果过滤:

$request['sales_channel'] = ['Non-Amazon', 'Amazon.com'];

Results would return:结果将返回:

sales_channel | total
--------------+----------------
Non-Amazon    | 19.99
Non-Amazon    | 0.00
Amazon.com    | 11.00

You can try something like this:你可以尝试这样的事情:

$request = $request->data;
$orders = Orders::query();

if ($request['sales_channel']) {
    $orders = $orders->whereIn('sales_channel', $request['sales_channel']);
}else{ 
    //the initial query
    $orders = $orders->where('sales_channel', '=', 'Non-Amazon')->orWhere(function ($query) {
        $query->where('total', '>', 0);
    });
}

//check if the orders are filtered correctly
var_dump($orders->get());

use when使用时

$orders = Orders::when(($request->sales_channel=="Non-Amazon"),function ($query)use($request){
                 $query->where('sales_channel', $request->sales_channel);
                 })->when(($request->sales_channel!="Non-Amazon"),function ($query)use($request){
                 $query->where('total', '>', 0);
                })->get();

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

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