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

时间:2018-06-02 22:07:35

标签: php laravel eloquent

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

<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查询来从数据库中获取此请求的数据,当我选择某个选项时它不是问题,但我遇到了用户选择“全部”的问题。选项......如何在Laravel控制器中处理它?<​​/ p>

我创建了类似的东西:

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-&gt;提供者为0时如何获取所有数据而没有make if {} else {}每次?

有没有办法做到这一点?

2 个答案:

答案 0 :(得分: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 :(得分:1)

我猜您可以将代码简化为

{{1}}