Laravel - Collections - Method Unless

The unless method will execute the given callback unless the first argument given to the method evaluates to true:

    
    $collection = collect([1, 2, 3]);
    
    $collection->unless(true, function ($collection) {
        return $collection->push(4);
    });
    
    $collection->unless(false, function ($collection) {
        return $collection->push(5);
    });
    
    $collection->all();
    
    // [1, 2, 3, 5]
	

A second callback may be passed to the unless method. The second callback will be executed when the first argument given to the unless method evaluates to true:

    
    $collection = collect([1, 2, 3]);
    
    $collection->unless(true, function ($collection) {
        return $collection->push(4);
    }, function ($collection) {
        return $collection->push(5);
    });
    
    $collection->all();
    
    // [1, 2, 3, 5]
	

For the inverse of unless, see the when method.