Last active
April 28, 2018 21:57
-
-
Save AdenFraser/c7a66a668cc4bbba20967de13324af23 to your computer and use it in GitHub Desktop.
Laravel firstOr Collection Macro
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Illuminate\Support\Collection; | |
/* | |
* Get the first item. Performs callback if the item was not found. | |
* | |
* @return mixed | |
*/ | |
Collection::macro('firstOr', function (callable $callback) { | |
if (! is_null($item = $this->first())) { | |
return $item; | |
} | |
return $callback(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment