Last active
March 22, 2019 17:47
-
-
Save mustafaaloko/faec02c969b9e40e242eac0b1855a60d to your computer and use it in GitHub Desktop.
Laravel Macro Example
This file contains 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; | |
Collection::macro('evens', function() { | |
return $this->filter(function($value) { | |
return $value % 2 === 0; | |
}); | |
}); | |
// Now the above can be used as below: | |
$collection = collect([3, 5, 8, 2, 1, 6]); | |
$collection->evens(); | |
// [8, 2, 6] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment