阅读(4277) (7)

Laravel 8 except() {#collection-method}

2021-07-14 11:58:39 更新

except 方法返回集合中除了指定键之外的所有集合项:

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

$filtered = $collection->except(['price', 'discount']);

$filtered->all();

// ['product_id' => 1] 

except 对应的是 only 方法。

技巧:这个方法的行为当使用 Eloquent Collections 时会被重写。