Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PHP

laravel use variable inside callback function

// You should use the "use" keyword to use a variable in the anonymous scope
$current_date = '2021-01-14';
$schedules = Schedule::all();

// Here's an example of filtering a collection using a variable defined
// outside the anonymous scope. Here we used $current_date within the callback
// function to keep only the schedules with the same date as the current date
$filtered = $schedules->filter(function ($schedule) use ($current_date) {
        return $schedule->date == $current_date;
};
 
PREVIOUS NEXT
Tagged: #laravel #variable #callback #function
ADD COMMENT
Topic
Name
5+7 =