阅读(4196) (3)

Laravel 8 first() {#collection-method}

2021-07-01 14:28:57 更新

first 方法返回集合中通过指定条件测试的第一个元素:

collect([1, 2, 3, 4])->first(function ($value, $key) {
    return $value > 2;
});

// 3

你也可以不传入参数调用 first 方法来获取集合中的第一个元素。如果集合为空,则会返回 null

collect([1, 2, 3, 4])->first();

// 1