💎一站式轻松地调用各大LLM模型接口,支持GPT4、智谱、豆包、星火、月之暗面及文生图、文生视频 广告
## 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 方法。