#Using when conditional helper for Eloquent Builder

3 messages · Page 1 of 1 (latest)

placid cairn
#

Can I not pass the $dateRange array into the closure for the when function? PHPStan is telling me the following errors.

         (callable(App\Builders\UserBuilder<App\Models\User>, mixed): App\Builders\UserBuilder<App\Models\User>)|null,                         
         Closure(Illuminate\Database\Eloquent\Builder, array): App\Builders\UserBuilder<App\Models\User> given.                                        
         🪪  argument.type                                                                                                                                     
         💡 Type #1 from the union: Type array of parameter #2 $dateRange of passed callable needs to be same or wider than parameter type mixed of accepting  
            callable.                                                                                                                                          
  40     Parameter #3 $value of method App\Builders\UserBuilder<App\Models\User>::whereDate() expects DateTimeInterface|string|null, mixed given.      
         🪪  argument.type                                                                                                                                     
  41     Parameter #3 $value of method App\Builders\UserBuilder<App\Models\User>::whereDate() expects DateTimeInterface|string|null, mixed given.      
         🪪  argument.type```

```return User::query()
    ->with('currentEmployment')
    ->when(
        $this->getAppliedFilterWithValue('Employment'),
        /** @param array{minDate: string, maxDate: string}  $dateRange */
        fn (Builder $query, array $dateRange) => $query
            ->whereDate('user_employments.started_at', '>=', $dateRange['minDate'])
            ->whereDate('user_employments.ended_at', '<=', $dateRange['maxDate'])
    );```
glad nova
placid cairn
#

@glad nova So that means I can't have additional parameters like dateRange?