Skip to content

Variable in a Collection request #2539

Closed Answered by alcaeus
zane-shus asked this question in Q&A
Discussion options

You must be logged in to vote

Closures don't automatically capture the current scope, so you have two ways of fixing this. If using a long closure, make sure to bind any variables used within the closure with the use keyword:

$page = Track::raw(
    function ($collection) use ($value) {
        return $collection->aggregate([['$match' => ['my_id' => $value ]]]);
    }
);

Alternatively, use a short closure which automatically captures the local scope:

$page = Track::raw(
    fn ($collection) => 
        $collection->aggregate([['$match' => ['my_id' => $value ]]])
);

I'll assume that you changed the code for brevity in this issue, but for the sake of completeness I'll mention that the query you're running neither requir…

Replies: 3 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by divine
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
1 reply
@zane-shus
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
2 participants
Converted from issue

This discussion was converted from issue #2538 on May 17, 2023 13:42.