Skip to content

Instantly share code, notes, and snippets.

@TsuiAnthonYVR
Last active November 18, 2016 21:21
Show Gist options
  • Save TsuiAnthonYVR/8436c35c74ca9e4a42ff38f2c7ab696b to your computer and use it in GitHub Desktop.
Save TsuiAnthonYVR/8436c35c74ca9e4a42ff38f2c7ab696b to your computer and use it in GitHub Desktop.
Adding custom macros into Laravel
<?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