doufu4333 2018-06-02 22:07
浏览 24
已采纳

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

AT front-end I have some select buttons (choose an provider, choose a user, date range period).

<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>

so now I need to build Laravel query to fetch data from a database for this requests and it's not a problem when I choose some option but I have a problem where user choose 'ALL' option... how to handle that in Laravel controller?

I create something like:

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

Is there a more elegant way than this I use?

So how to fetch all data when request->provider is 0 without make if {} else {} each time ?

Is there some way to do that ?

  • 写回答

2条回答 默认 最新

  • dongmangsha7354 2018-06-02 22:35
    关注

    Use 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()
    

    For L5.1 I'd do this:

    $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条)

报告相同问题?

悬赏问题

  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题