Laravel:带有Elouquent的动态where子句 [英] Laravel: dynamic where clause with Elouquent
本文介绍了Laravel:带有Elouquent的动态where子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在调用带有动态搜索参数的URL.我该如何形成正确的口才查询?
I am calling URL with search params which are dynamic. How could I form proper Eloquent query?
理论上:
- 查询
- 查询其中(someParam1)
- 查询where(someParam2)
- 查询orderby(someParam3)
- 查询获取
- query
- query where(someParam1)
- query where(someParam2)
- query orderby(someParam3)
- query get
我需要这种结构,因此如果 param存在,我可以使用where子句. 如果Laravel中还有其他方法,请告诉我.
I need this kind of structure so I can use where clause if param exists. If there is some other way in Laravel, please let me know.
推荐答案
使用Laravel很容易.只需执行以下操作即可:
It's easy with Laravel. Just do something like this:
$query = User::query();
if ($this == $that) {
$query = $query->where('this', 'that');
}
if ($this == $another_thing) {
$query = $query->where('this', 'another_thing');
}
if ($this == $yet_another_thing) {
$query = $query->orderBy('this');
}
$results = $query->get();
这篇关于Laravel:带有Elouquent的动态where子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文