ThinkChat2.0新版上线,更智能更精彩,支持会话、画图、视频、阅读、搜索等,送10W Token,即刻开启你的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 方法。