使用Laravel中的另一个关联字段为dateRange创建条件

时间:2019-03-30 08:13:30

标签: laravel if-statement conditional-statements

我正在尝试创建一个销售过滤页面,用户可以在其中设置dateRange,如果他们愿意,还可以将货币和客户名称设置为过滤器的一部分。

enter image description here

public function filterSale(Request $request)
{
    $customer = \DB::table('companies')->where('category', '=', 'customer')->pluck('comp_name', 'id')->all();
    $currencies = \DB::table('currencies')->orderBy('default', 'desc')->pluck('acronym', 'id')->all();

    $currency = $request['currency_id'];
    $company = $request['company_id'];

    if ($request->from_date != '' && $request->to_date != '' || $request['currency_id'] != ''
        || $request['company_id'] != '') {
        $sales = Sales::where('publish', '=', 1)
            ->whereBetween('created_at', array($request->from_date, $request->to_date))
            ->where('currency_id', '=', $currency)
            ->where('company_id', '=', $company)
            ->get();

        return view('reports.salesReport', compact('sales', $sales))
            ->with('currency', $currency)
            ->with('company', $company)
            ->with('customer', $customer)
            ->with('currencies', $currencies);
    }
}

我要实现的目标是,如果用户设置dateRage并将货币和客户保留为空,则数据将显示所有内容,而不管其货币及其所属的客户。

客户或日期+货币或日期范围或或货币+客户+日期。

我该如何实现?

非常感谢您!

1 个答案:

答案 0 :(得分:2)

有两种方法可以实现此目的。

第一个方法是将条件包装在if语句中

if ($request->from_date != '' && $request->to_date != '') {
    $salesQuery = Sales::where('publish', 1)
        ->whereBetween('created_at', [$request->from_date, $request->to_date]);

    if ($request->input('current_id')) {
        $salesQuery->where('currency_id', $request->input('currency_id'));
    }

    if ($request->input('company_id')) {
        $salesQuery->where('company_id', $request->input('company_id'));
    }

    $sales = $salesQuery->get();

}

或者,您可以使用when()

if ($request->from_date != '' && $request->to_date != '') {
    $sales = Sales::where('publish', 1)
        ->whereBetween('created_at', [$request->from_date, $request->to_date])
        ->when($request->input('current_id'), function ($query) use ($request) {
            $query->where('currency_id', $request->input('currency_id'));
        })
        ->when($request->input('company_id'), function ($query) use ($request) {
            $query->where('company_id', $request->input('company_id'));
        })
        ->get();
}

最后,将return语句更改为仅使用compact(),以便在不包括日期的情况下不会引发错误,这意味着整个控制器方法将类似于:

public function filterSale(Request $request)
{
    $customer = \DB::table('companies')->where('category','customer')->pluck('comp_name', 'id');
    $currencies = \DB::table('currencies')->orderBy('default', 'desc')->pluck('acronym', 'id');

    if ($request->from_date != '' && $request->to_date != '') {

        $sales = Sales::where('publish', 1)
            ->whereBetween('created_at', [$request->from_date, $request->to_date])
            ->when($request->input('current_id'), function ($query) use ($request) {
                $query->where('currency_id', $request->input('currency_id'));
            })
            ->when($request->input('company_id'), function ($query) use ($request) {
                $query->where('company_id', $request->input('company_id'));
            })
            ->get();

    }

    return view('reports.salesReport', compact('sales', 'currency', 'company', 'customer', 'currencies'));
}

注意事项:您无需在all()之后致电pluck()