Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

MrMoto9000's avatar

Inline query scope with parameter

How can I pass a parameter to an inline query scope?

   $shows = Show::upcoming()->with(
        ['events' => function($q, $reward) {
            $q->restrictDays($reward->ticket_restrictions);
        }]
    )->get();

This causes the following error:

Too few arguments to function App\Http\Controllers\EventController::App\Http\Controllers{closure}(), 1 passed in /vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php on line 629 and exactly 2 expected

0 likes
2 replies
tykus's avatar

If you are using PHP 7.4 or above, you can use short Closures (so do not need to worry about variable scope within the Closure):

$shows = Show::upcoming()
    ->with(['events' => fn ($q) => $q->restrictDays($reward->ticket_restrictions)])
    ->get();

Please or to participate in this conversation.