Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

riseabove2_2's avatar

Laravel Nova Image::make('Image') dynamic s3 disk

I have different folders in my aws s3 for different types of images, so I have multiple s3 disks in my filesystems.php. I need to be able to dynamically insert the correct driver depending on where the image was stored. In the photo model, there is a column called folder_path, which stores the correct driver ('s3-images' or 's3-crusades') depending on what was used during the original upload.

filesystems.php

's3-images' => [
            'driver' => 's3',
            'key'    => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => 'cfanweb.images',
            'url'    => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
            'visibility' => 'public'
        ],

        's3-crusades' => [
            'driver' => 's3',
            'key'    => env('AWS_ACCESS_KEY_ID'),
            'secret' => env('AWS_SECRET_ACCESS_KEY'),
            'region' => env('AWS_DEFAULT_REGION'),
            'bucket' => 'cfanweb.crusades',
            'url'    => env('AWS_URL'),
            'endpoint' => env('AWS_ENDPOINT'),
            'use_path_style_endpoint' => env('AWS_USE_PATH_STYLE_ENDPOINT', false),
            'throw' => false,
            'visibility' => 'public'
        ],

in my photo.php I would like to do something like ->disk(function () { do something } but it seems like you can't use a function inside the Image::make ->disk. I get "message: "Illegal offset type", exception: "TypeError",…" I would like to do something similar to what I did to retrieve the thumbnail:

Image::make('Image')
                ->disk(function ($model) {
                    dd($model->folder_path);
                })

or

Image::make('Image')
                ->disk(function ($value) {
                    if($this->resource->folder_path) {
                        $disk = $this->resource->folder_path;
                        return $disk;
                    } else {
                        $disk = 's3-images';
                        return $disk;
                    }
                })
                ->thumbnail(function ($value, $disk, $model) {
                    if ($value) {
                        $url = Storage::disk($this->folder_paths)->url($value);
                        return $url ?? null;
                    } else {
                        return null;
                    }
                })
                ->preview(function ($value, $disk) {
                    if ($value) {
                        $url = Storage::disk($disk)->url($value);
                        return $url ?? null;
                    } else {
                        return null;
                    }
                })
                ->store(new PhotoUpload)
                ->creationRules('required')
                ->readonly(function ($request) {
                    return $request->isUpdateOrUpdateAttachedRequest();
                }),

I even tried setting the variable before the return in the function fields (however bad it looked), but when you try to use the variable in the ->disk($variable) it doesn't work. Anybody have any ideas

0 likes
2 replies
tisuchi's avatar

@riseabove2_2 (Untested) does it work for you?

Image::make('Image')
    ->resolve(function ($value, $model) {
        $disk = $model->folder_path ?? 's3-images';
        return Storage::disk($disk)->url($value);
    })
    ->thumbnail(function ($value, $disk, $model) {
        if ($value) {
            $url = Storage::disk($this->folder_paths)->url($value);
            return $url ?? null;
        } else {
            return null;
        }
    })
    ->preview(function ($value, $disk) {
        if ($value) {
            $url = Storage::disk($disk)->url($value);
            return $url ?? null;
        } else {
            return null;
        }
    })
    ->store(new PhotoUpload)
    ->creationRules('required')
    ->readonly(function ($request) {
        return $request->isUpdateOrUpdateAttachedRequest();
    }),
riseabove2_2's avatar

@tisuchi Thanks for the reply. Sorry, I forgot to mention I am using laravel 9 and nova 4.0. ->resolve seems to no longer be available. However, there is a resolveUsing method, but it doesn't seem to allow me to set the disk value properly. https://nova.laravel.com/docs/4.0/resources/fields.html?#ui-avatar-field There is also dynamic field methods you can create, https://nova.laravel.com/docs/4.0/resources/fields.html#dynamic-field-methods, but seem to not be able to get that to work right either.

Please or to participate in this conversation.