【发布时间】:2023-02-21 02:32:00
【问题描述】:
我有用户和客户表。
我的每个用户都有一组预定义的查询。例如basic 用户,只能看到类型为new 和potential 的客户。
我如何编写一个查询,让用户选择只查看 potential 客户端,或者只查看具有特定名称的客户端?
我现在的做法很简单
$defaultQuery = [['type', 'new'],['type', 'potential']];
$customQuery = ['type', 'new']; // I would pass this, can be null
$customers = Customer::where($defaultQuery[0][0], $defaultQuery[0][1])->orWhere($defaultQuery[1][0], $defaultQuery[1][1])->get();
$customersToReturn = [];
if ($customQuery) {
foreach($customers as $customer) {
if ($customer[$customQuery[0]] == $customQuery[1]) {
$customerToReturn[] = $customer;
}
}
} else {
$customersToReturn = $customers;
}
return $customersToReturn;
我知道有一个 filter() 函数,但我想用子查询进行一个查询。我怎样才能做到这一点?
【问题讨论】:
-
是否需要将2个
where组合成一个,然后有条件的申请orWhere? laravel.com/docs/10.x/queries#or-where-clauses -
@OstapBrehin 我的自定义查询必须改进主查询的结果。它不会或在哪里。