Skip to content

Instantly share code, notes, and snippets.

@gander
Last active November 5, 2021 09:58
Show Gist options
  • Save gander/c7d8af73cc715fa8e12d5c4be63c4975 to your computer and use it in GitHub Desktop.
Save gander/c7d8af73cc715fa8e12d5c4be63c4975 to your computer and use it in GitHub Desktop.
Laravel when Closure $callback snippets
<?php
$callback = function () {
};
collect()->when($callback, static function (\Illuminate\Support\Collection $collection, \Closure $callback) {
return \Illuminate\Support\Collection::wrap($callback($collection));
}); // Same as \DB::query()->get(), Model::query()->get()
\DB::query()->when($callback, static function (\Illuminate\Database\Query\Builder $query, \Closure $callback) {
$callback($query);
}); // Same as Model::query()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment