如果请求为0,则Laravel查询获取所有请求

问题描述 投票:1回答:2

在前端我有一些选择按钮(选择提供商,选择用户,日期范围时段)。

<select class="form-control" id="provider">
      <option value="0">ALL</option>
      <option value="1">First</option>
      <option value="2">Second</option>
      <option value="3">Third</option>
      <option value="4">Fourth</option>

    </select>
<select class="form-control" id="res_user">
          <option value="0">ALL</option>
          <option value="13">James</option>
          <option value="27">Perica Aleksov</option>
          <option value="30">sad</option>
          <option value="32">Test Restaurant</option>
          <option value="33">dsfdf</option>
        </select>

所以现在我需要构建Laravel查询以从数据库中获取此请求的数据,当我选择某个选项时它不是问题,但我有一个问题,用户选择'ALL'选项...如何在Laravel控制器中处理它?

我创造了类似的东西:

if ($request->provider == 0 AND $request->res_user== 0) {
    $vouchers = Voucher::latest()
        ->where('user_id',$request->account)
        // ->where('created_by',$request->res_user)
        // ->where('source',$request->provider)
        ->where('created_at', '>=',  Carbon::parse($request->startDate))
        ->where('created_at', '<=',  Carbon::parse($request->endDate))
        ->get();
} elseif ($request->provider == 0) {
    $vouchers = Voucher::latest()
        ->where('user_id',$request->account)
        ->where('created_by',$request->res_user)
        // ->where('source',$request->provider)
        ->where('created_at', '>=',  Carbon::parse($request->startDate))
        ->where('created_at', '<=',  Carbon::parse($request->endDate))
        ->get();
} elseif ($request->res_user== 0) {
    $vouchers = Voucher::latest()
        ->where('user_id',$request->account)
        // ->where('created_by',$request->res_user)
        ->where('source',$request->provider)
        ->where('created_at', '>=',  Carbon::parse($request->startDate))
        ->where('created_at', '<=',  Carbon::parse($request->endDate))
        ->get();
} else {
    $vouchers = Voucher::latest()
        ->where('user_id',$request->account)
        ->where('created_by',$request->res_user)
        ->where('source',$request->provider)
        ->where('created_at', '>=',  Carbon::parse($request->startDate))
        ->where('created_at', '<=',  Carbon::parse($request->endDate))
        ->get();
}

有没有比我使用的更优雅的方式?

那么当request-> provider为0时如何获取所有数据而不是make if {} else {}每次?

有办法做到这一点吗?

php laravel eloquent
2个回答
2
投票

使用when

$vouchers = Voucher::latest()
    ->when($request->provider != 0, function ($q) use ($request) {
        $q->where('source', $request->provider);
    })
    ->when($request->res_user != 0, function ($q) use ($request) {
        $q->where('created_by', $request->res_user);
    })
    ->where('created_at', '>=',  Carbon::parse($request->startDate))
    ->where('created_at', '<=',  Carbon::parse($request->endDate))
    ->get()

对于L5.1我会这样做:

$baseQuery = Voucher::latest()
    ->where('created_at', '>=',  Carbon::parse($request->startDate))
    ->where('created_at', '<=',  Carbon::parse($request->endDate));

if ($request->provider != 0) {
    $baseQuery = $baseQuery->where('source', $request->provider);
}
if ($request->res_user != 0) {
    $baseQuery = $baseQuery->where('created_by', $request->res_user);
}

$vouchers = $baseQuery->get();

1
投票

我想你可以简化你的代码

$vouchers = Voucher::latest()
    ->where('user_id', $request->account)
    ->where('created_at', '>=', Carbon::parse($request->startDate))
    ->where('created_at', '<=', Carbon::parse($request->endDate));
/* Above don't need any if checks this criteria will always be there */
if ($request->provider == 0 && $request->res_user != 0) {
    $vouchers->where('created_by', $request->res_user);
} elseif ($request->res_user == 0 && $request->provider != 0) {
    $vouchers->where('source', $request->provider);
} else {
    $vouchers->where('created_by', $request->res_user)
             ->where('source', $request->provider);
}
$vouchers = $vouchers->get();
© www.soinside.com 2019 - 2024. All rights reserved.