如果请求为 0,Laravel 查询获取全部
Posted
技术标签:
【中文标题】如果请求为 0,Laravel 查询获取全部【英文标题】:Laravel query to get all if request is 0 【发布时间】:2018-11-12 15:59:14 【问题描述】:在前端我有一些选择按钮(选择提供商、选择用户、日期范围)。
<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 的情况下获取所有数据?
有什么办法吗?
【问题讨论】:
【参考方案1】:使用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();
【讨论】:
我正在使用 laravel 5.1 ...所以这是一个优雅的解决方案,但不适用于 5.1 版本 我明白了。更新了答案(基于 M Khalid Junaid 的答案稍微更优雅的解决方案) 无疑它很优雅,我喜欢这两种方法【参考方案2】:我猜你可以将你的代码简化为
$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 查询获取全部的主要内容,如果未能解决你的问题,请参考以下文章