AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
## whenEmpty 方法是当集合为空时,将执行给定的回调函数: whenEmpty 方法是当集合为空时,将执行给定的回调函数: ``` $collection = collect(['michael', 'tom']); $collection->whenEmpty(function ($collection) { return $collection->push('adam'); }); $collection->all(); // ['michael', 'tom'] $collection = collect(); $collection->whenEmpty(function ($collection) { return $collection->push('adam'); }); $collection->all(); // ['adam'] $collection = collect(['michael', 'tom']); $collection->whenEmpty(function ($collection) { return $collection->push('adam'); }, function ($collection) { return $collection->push('taylor'); }); $collection->all(); // ['michael', 'tom', 'taylor']Copy ``` 与 whenEmpty 相反的方法,请查看 whenNotEmpty 方法。