Aitor Ballesteros
Aitor Ballesteros

Reputation: 41

Request doesn't work when I use filter() in collection Laravel

I am trying to get a filter in a collection, but I get an error:

"code": 500, "error_msg": "Undefined variable: request"

This is my code where I have the error. The request works in any part of the code, but not here...

   $filterKarateka = $karatekasInMarket ->filter(function($item) {           
      return $item->id == $request->id_karateka; 
                    })->first();

Upvotes: 1

Views: 526

Answers (1)

nice_dev
nice_dev

Reputation: 17805

You have to use as in include variables that are out of scope of the callback. So it would be

filter(function ($item) use ($request){

Upvotes: 1

Related Questions