Last active
November 18, 2016 21:21
-
-
Save TsuiAnthonYVR/8436c35c74ca9e4a42ff38f2c7ab696b to your computer and use it in GitHub Desktop.
Adding custom macros into Laravel
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 | |
namespace App\Providers; | |
use Illuminate\Support\ServiceProvider; | |
use Illuminate\Support\Collection; | |
class AppServiceProvider extends ServiceProvider | |
{ | |
/** | |
* Bootstrap any application services. | |
* | |
* @return void | |
*/ | |
public function boot() | |
{ | |
Collection::macro('ifEmpty', function (callable $callback): Collection { | |
if ($this->isEmpty()) { | |
$callback($this); | |
} | |
return $this; | |
/* what does $this mean in this scope? | |
collect()->ifEmpty(function($e) {error_log('empty '.get_class($e));}) | |
empty Illuminate\Support\Collection | |
*/ | |
}); | |
} | |
/** | |
* Register any application services. | |
* | |
* @return void | |
*/ | |
public function register() | |
{ | |
// | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment