简体   繁体   English

如何在 scopeFilter 查询中传递 2 个参数

[英]How to pass 2 parameters in a scopeFilter query

How do I go about passing two parameters in a filter query?如何在过滤器查询中传递两个参数?

The below works but I want to use the $filters[$serviceDate ] instead of hardcoding the date in the last line下面的作品,但我想使用 $filters[$serviceDate ] 而不是硬编码最后一行中的日期

 'filters' => Request::all('serviceDate',  'mealType'),
 public function scopeFilter($query, array $filters)
{
        $query
            ->when( $filters['mealType'] ?? null, function ($query, $mealType) {
                $query->whereDoesntHave('student_meals', fn ($query) => 
                    $query->where('meal_type_id', $mealType )
                        ->where('void', false)
                        ->where('date_served', '2022-06-19')  
            });          
}

I've tried我试过了

 ->when( $filters['mealType'] ?? null, function ($query, $mealType, $serviceDate) {
                $query->whereDoesntHave('student_meals', fn ($query) => 
                    $query->where('meal_type_id', $mealType )
                        ->where('void', false)
                        ->where('date_served', $serviceDate));

and get the error:并得到错误:

Too few arguments to function App\Models\Student::App\Models{closure}(), 2 passed in /Applications/XAMPP/xamppfiles/htdocs/Sos/vendor/laravel/framework/src/Illuminate/Conditionable/Traits/Conditionable.php on line 30 and exactly 3 expected函数 App\Models\Student::App\Models{closure}() 的参数太少,在 /Applications/XAMPP/xamppfiles/htdocs/Sos/vendor/laravel/framework/src/Illuminate/Conditionable/Traits/Conditionable 中传递了 2 个.php 在第 30 行,预计正好 3

I've tried我试过了

->when( ($filters['mealType'] && $filters['serviceDate']) ?? null, function ($query, $mealType, $serviceDate) {
                $query->whereDoesntHave('student_meals', fn ($query) => 
                    $query->where('meal_type_id', $mealType )
                        ->where('void', false)
                        ->where('date_served', $serviceDate));

and get the error:并得到错误:
Undefined array key "mealType"未定义的数组键“mealType”

I know I'm missing something basic but struggling to figure it out.我知道我缺少一些基本的东西,但很难弄清楚。 Any help is much appreciated.任何帮助深表感谢。

Yes, you are missing how to inherit variables from the parent scope .是的,您缺少如何从父范围继承变量 That's is basic knowledge about anonymous function in PHP.这是PHP中匿名函数的基本知识。

You need to use ($mealType) , so the correct code should be like:你需要use ($mealType) ,所以正确的代码应该是这样的:

public function scopeFilter($query, array $filters)
{
    $mealType = $filters['mealType'] ?? null;
    $serviceDate = $filters['serviceDate'] ?? null;
    $query
        ->when($mealType, function($query) use ($mealType,$serviceDate) {
            $query->whereDoesntHave('student_meals', fn($query) => $query->where('meal_type_id', $mealType)
                ->where('void', false)
                ->where('date_served', $serviceDate)
            );
        });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM