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

warpig's avatar
Level 12

Displaying a picture with a Filament Resource

Want to display a picture with this method on model TextWidget.php, im using a Filament resource because there are some other things involved. I want the string so i can use the path on the view on an href

    public static function getImage(string $key): string
    {
        $widget = \Illuminate\Support\Facades\Cache::get('text-widget-' . $key, function() use ($key) {
            return  TextWidget::query()
                ->where('key', '=', $key)
                ->where('active', '=', 1)
                ->first();
        });
            
            if ($widget) {
                return $widget->image;
            }
            return '';
    }

As i receive it in the view

            <a class="font-bold text-gray-800 uppercase hover:text-gray-700 text-5xl" 
                href="/storage/{{\App\Models\TextWidget::getImage('header')}}">
            </a>

Im not seeing anything displayed though. The Network tab shows this "/storage/JrBj9440UMUUG4SF5hxteMm49306jD-metac2lja29mbWV0YWwtaGVhZGVyLTAxLnBuZw==-.png"

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('key')
                    ->required()
                    ->maxLength(255),
                Forms\Components\FileUpload::make('image'),
                Forms\Components\TextInput::make('title')
                    ->required()
                    ->maxLength(2048),
                Forms\Components\RichEditor::make('content'),
                Forms\Components\Toggle::make('active')
                    ->required(),
            ])->columns(1);
    }

thanks

0 likes
3 replies
tisuchi's avatar

@warpig If you try this, does it work?

if ($widget && $widget->image) {
        return Storage::url($widget->image);
}

Don't forget to import \Illuminate\Support\Facades\Storage.

2 likes
tisuchi's avatar
tisuchi
Best Answer
Level 70

@warpig Wait a minute!

Are you trying to display image in <a> tag instead of <image.... tag?

Your code suppose to generate a link instead of displaying image, correct?

<a class="font-bold text-gray-800 uppercase hover:text-gray-700 text-5xl" 
                href="/storage/{{\App\Models\TextWidget::getImage('header')}}">
            </a>
2 likes

Please or to participate in this conversation.