LaravelPackages.net
Acme Inc.
Toggle sidebar
josephlavin/tap

Stand alone port of Laravel's tap method.

21.122
2
v1.0.0
About josephlavin/tap

josephlavin/tap is a Laravel package for stand alone port of laravel's tap method.. It currently has 2 GitHub stars and 21.122 downloads on Packagist (latest version v1.0.0). Install it with composer require josephlavin/tap. Discover more Laravel packages by josephlavin or browse all Laravel packages to compare alternatives.

Last updated

josephlavin/tap

Build Status

A stand alone port of Laravel's tap method (inspired by Ruby). This package will add a tap method to the global namespace. For more information see Taylor Otwell's explanation of tap.

Installation

composer require josephlavin/tap

Example

You need to create a model and commit it to the database using save:

function createAndSaveModel($attributes)
{
    $model = new Model($attributes);
    
    $model->save();
    
    return $model;
}

The same code can be simplified utilizing tap:

function createAndSaveModel($attributes)
{
    return tap(new Model($attributes), function (Model $model) {
        $model->save();
    });
}

Utilizing the proxy feature it can be further simplified:

function createAndSaveModel($attributes)
{
    return tap(new Model($attributes))->save();
}
Comments