1

I'm using spatie/async to do async uploading of photos to AWS S3. I'm trying to use some facades from within the closures. For example:

foreach ($images as $image) {
    ...

    $pool->add(function () use ($encodedImage, $previewImage, $post, $extension, $realPath, $pictureCount) {
            $resizedImage = Image::make($realPath)->fit(400, 400);
            ...
            Storage::disk('s3')->put($file_path, $resizedImage->__toString(), ['visibility' => 'public']);
            ...
            return $file_path;
        })->then(function ($s3Path) use ($s3Urls) {
            ...
        })->catch(function (Throwable $exception) {
            throw $exception;
        });
    }
    ...
}

It fails, and I receive the following error in my storage/logs/laravel.log file:

[2020-12-11 03:32:06] local.ERROR: A facade root has not been set.

#0 closure://function () use ($encodedImage, $previewImage, $post, $extension, $realPath, $pictureCount) {
                $resizedImage = \Intervention\Image\Facades\Image::make($realPath)->fit(400, 400);
...

I've seen some answers regarding "A facade root has not been set.", and it seems like the solution in some cases is to uncomment $app->withFacades(); in bootstrap/app.php - however, I don't have that in app.php, and trying to add it results in Call to undefined method Illuminate\Foundation\Application::withFacades().

I'm guessing I get this error because, within the closure, Laravel can't resolve \Intervention\Image\Facades\Image? How can I fix this, or is using facades in closures not supported?

Edit: to clarify, I can use this Image facade without any issues outside of the closure.

Karl Hill
  • 12,937
  • 5
  • 58
  • 95
ChristianF
  • 1,735
  • 4
  • 28
  • 56
  • did you register the service provider for intervention? – lagbox Dec 11 '20 at 03:59
  • Whats happens if use fully qualified class name? like `\Intervention\Image\Facades\Image` – Lessmore Dec 11 '20 at 04:07
  • Same error using the fully qualified name. In terms of registering the service provider for intervention, I followed http://image.intervention.io/getting_started/installation#laravel, which says to add `Intervention\Image\ImageServiceProvider::class` to the `$providers` array in `config/app.php`, and add `'Image' => Intervention\Image\Facades\Image::class` to the `$aliases` array. The intervention docs say that the Image class will be autoloaded after those steps. Also, I can also use `Intervention\Image\Facades\Image` outside of the Closure without any problems – ChristianF Dec 11 '20 at 04:16

0 Answers0