企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持知识库和私有化部署方案 广告
## whenNotEmpty 方法当集合不为空时,将执行给定的回调函数: whenNotEmpty 方法当集合不为空时,将执行给定的回调函数: ``` $collection = collect(['michael', 'tom']); $collection->whenNotEmpty(function ($collection) { return $collection->push('adam'); }); $collection->all(); // ['michael', 'tom', 'adam'] $collection = collect(); $collection->whenNotEmpty(function ($collection) { return $collection->push('adam'); }); $collection->all(); // [] $collection = collect(); $collection->whenNotEmpty(function ($collection) { return $collection->push('adam'); }, function ($collection) { return $collection->push('taylor'); }); $collection->all(); // ['taylor'] ``` 与 whenNotEmpty 相反的方法,请查看 whenEmpty 方法