合规国际互联网加速 OSASE为企业客户提供高速稳定SD-WAN国际加速解决方案。 广告
## 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 方法。