laravel use variable inside callback function

Solutions on MaxInterview for laravel use variable inside callback function by the best coders in the world

showing results for - "laravel use variable inside callback function"
Alessia
27 Aug 2016
1// You should use the "use" keyword to use a variable in the anonymous scope
2$current_date = '2021-01-14';
3$schedules = Schedule::all();
4
5// Here's an example of filtering a collection using a variable defined
6// outside the anonymous scope. Here we used $current_date within the callback
7// function to keep only the schedules with the same date as the current date
8$filtered = $schedules->filter(function ($schedule) use ($current_date) {
9        return $schedule->date == $current_date;
10};