Laravel - Collections - Method Only

The only method returns the items in the collection with the specified keys:

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

For the inverse of only, see the except method.

This method's behavior is modified when using Eloquent Collections.