Skip to content

Instantly share code, notes, and snippets.

@spaceemotion
Created July 22, 2018 20:16
Show Gist options
  • Select an option

  • Save spaceemotion/fba5f154d39f3cf9483e920868fe744c to your computer and use it in GitHub Desktop.

Select an option

Save spaceemotion/fba5f154d39f3cf9483e920868fe744c to your computer and use it in GitHub Desktop.

Revisions

  1. spaceemotion created this gist Jul 22, 2018.
    29 changes: 29 additions & 0 deletions DatabaseServiceProvider.php
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    <?php

    namespace App\Providers;

    use Illuminate\Database\Eloquent\Builder;

    class DatabaseServiceProvider extends ServiceProvider
    {
    /**
    * Bootstrap any application services.
    *
    * @return void
    */
    public function boot()
    {
    // Chunking that does not break with where clauses
    Builder::macro('chunkSimple', function (int $size, callable $callable) {
    $page = 1;

    while (($chunk = $this->limit($size)->get())->count() >= $size) {
    if ($callable($chunk, $page++) === false) {
    return false;
    }
    }

    return true;
    });
    }
    };