阅读(3024) (5)

Laravel 8 only() {#collection-method}

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

only 方法返回集合中所有指定键的集合项:

$collection = collect(['product_id' => 1, 'name' => 'Desk', 'price' => 100, 'discount' => false]);

$filtered = $collection->only(['product_id', 'name']);

$filtered->all();

// ['product_id' => 1, 'name' => 'Desk']

only 对应的是 except 方法。

技巧:此方法在使用 Eloquent Collections 时会被更改。