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

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果请求为0,则Laravel查询获取所有请求相关的知识,希望对你有一定的参考价值。

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

<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 {}每次?

有办法做到这一点吗?

答案

使用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();
另一答案

我想你可以简化你的代码

$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();

以上是关于如果请求为0,则Laravel查询获取所有请求的主要内容,如果未能解决你的问题,请参考以下文章

Laravel ajax存储请求errormessage,数据附加到视图

如果关系项不在 Laravel 的请求中,则删除它

Laravel 获取正文请求并避免查询字符串参数

提高查询速度Ionic / Laravel Post请求

在所有 Laravel api 请求中获取 401

在 Laravel 中获取请求值