我可以使用 Laravel 5 链接可变数量的查询范围吗
Can I chain a variable number of query scopes using Laravel 5
我的用户需要能够使用最多 5 个不同的参数查询数据库。我认为处理此问题的最佳方法是使用查询范围。然后将查询范围链接在一起。但是我不知道如何根据未知数量 (0-5) 的搜索参数来执行此操作。
我让它使用一个特定的搜索参数。
$thirtyDaysAgo = Carbon::now()->subDays(30)->toDateString();
$orders = Order::DateRange($thirtyDaysAgo)->get();
return view('orders/browse', compact('orders'));
如有任何帮助,我们将不胜感激。
编辑:更多信息
参数从表单发布到页面:
$input = Input::all();
dd($input);
产量
array:7 [▼
"_token" => "MX4gVmbON56f9Aa88kgn2Re68GoDrtDeR6phEJ30"
"orderType" => "1"
"orderNumber" => "1"
"timePeriod" => "0"
"orderStatus" => "0"
"sku" => ""
"customer" => "0"
]
编辑:添加查询范围
public function scopeDateRange($query, $range){
return $query->where('created_at', '>=', $range);
}
public function scopeOrderNumber($query, $orderNumber){
return $query->whereOrderNumber($orderNumber);
}
public function scopeCustomer($query, $customer){
return $query->whereCustomerId($customer);
}
public function scopeStatus($query, $status){
if($status == 'active'){
return $query->where('orderStatus_id', '!=', 15)->where('orderStatus_id', '!=', 10);
}elseif($status == 'complete'){
return $query->whereOrderStatusId(15);
}elseif($status == 'cancelled'){
return $query->whereOrderStatusId(10);
}
}
可以,只需循环用户输入字段,例如:
// 你现在有了这个,所以在调用 get 之前你可以链接
$thirtyDaysAgo = Carbon::now()->subDays(30)->toDateString();
// Remove the get call
$order = Order::DateRange($thirtyDaysAgo); // Or use Order::query()
// Loop other fields (exclude fields which are not required in query)
foreach(Request::except(['_token', 'other_field_name']) as $field => $value)
{
// Make sure $field (form fields) match with database filed names
$order->where($field, $value);
}
$result = $order->get(); // Run the query and get the result
这是一个想法,您可能需要根据自己的需要进行调整以使其适合。自己尝试或 post 最相关的信息。这不是在使用 scopeMethods
,但您可以使用它来实现您的目标。
从表面上看,您只想检查参数是否为空,如果是,您可以只 return 查询而不执行范围检查:
public function scopeDateRange($query, $range){
if (!empty($range)) {
return $query->where('created_at', '>=', $range);
}
else {
return $query;
}
}
然后,您可以将它们全部链接在一起,作用域函数将自行决定是否过滤查询。
$orders = Order::dateRange($range)->orderNumber($orderNumber)->customer($customer)->status($status)->get();
我的用户需要能够使用最多 5 个不同的参数查询数据库。我认为处理此问题的最佳方法是使用查询范围。然后将查询范围链接在一起。但是我不知道如何根据未知数量 (0-5) 的搜索参数来执行此操作。
我让它使用一个特定的搜索参数。
$thirtyDaysAgo = Carbon::now()->subDays(30)->toDateString();
$orders = Order::DateRange($thirtyDaysAgo)->get();
return view('orders/browse', compact('orders'));
如有任何帮助,我们将不胜感激。
编辑:更多信息
参数从表单发布到页面:
$input = Input::all();
dd($input);
产量
array:7 [▼
"_token" => "MX4gVmbON56f9Aa88kgn2Re68GoDrtDeR6phEJ30"
"orderType" => "1"
"orderNumber" => "1"
"timePeriod" => "0"
"orderStatus" => "0"
"sku" => ""
"customer" => "0"
]
编辑:添加查询范围
public function scopeDateRange($query, $range){
return $query->where('created_at', '>=', $range);
}
public function scopeOrderNumber($query, $orderNumber){
return $query->whereOrderNumber($orderNumber);
}
public function scopeCustomer($query, $customer){
return $query->whereCustomerId($customer);
}
public function scopeStatus($query, $status){
if($status == 'active'){
return $query->where('orderStatus_id', '!=', 15)->where('orderStatus_id', '!=', 10);
}elseif($status == 'complete'){
return $query->whereOrderStatusId(15);
}elseif($status == 'cancelled'){
return $query->whereOrderStatusId(10);
}
}
可以,只需循环用户输入字段,例如:
// 你现在有了这个,所以在调用 get 之前你可以链接 $thirtyDaysAgo = Carbon::now()->subDays(30)->toDateString();
// Remove the get call
$order = Order::DateRange($thirtyDaysAgo); // Or use Order::query()
// Loop other fields (exclude fields which are not required in query)
foreach(Request::except(['_token', 'other_field_name']) as $field => $value)
{
// Make sure $field (form fields) match with database filed names
$order->where($field, $value);
}
$result = $order->get(); // Run the query and get the result
这是一个想法,您可能需要根据自己的需要进行调整以使其适合。自己尝试或 post 最相关的信息。这不是在使用 scopeMethods
,但您可以使用它来实现您的目标。
从表面上看,您只想检查参数是否为空,如果是,您可以只 return 查询而不执行范围检查:
public function scopeDateRange($query, $range){
if (!empty($range)) {
return $query->where('created_at', '>=', $range);
}
else {
return $query;
}
}
然后,您可以将它们全部链接在一起,作用域函数将自行决定是否过滤查询。
$orders = Order::dateRange($range)->orderNumber($orderNumber)->customer($customer)->status($status)->get();