📜  laravel 在回调函数中使用变量 - PHP 代码示例

📅  最后修改于: 2022-03-11 14:54:30.350000             🧑  作者: Mango

代码示例1
// 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;
};