阅读(48) (3)

Laravel 8 take() {#collection-method}

2021-07-01 15:42:07 更新

take 方法返回给定数量项的新集合:

$collection = collect([0, 1, 2, 3, 4, 5]);

$chunk = $collection->take(3);

$chunk->all();

// [0, 1, 2] 

你也可以传递负整数从集合末尾获取指定数量的项:

$collection = collect([0, 1, 2, 3, 4, 5]);

$chunk = $collection->take(-2);

$chunk->all();

// [4, 5]