You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel Filament编辑与查看页面图片无法显示问题求助

Laravel Filament 用户图片在编辑/查看页无法显示的问题

我是Laravel Filament新手,在用户资源中配置了用户图片字段,该图片在列表页(Table)可正常显示,且能编辑更新,数据库中存储的图片路径也正确,但进入编辑页或查看页时,图片无法显示。

相关代码

<?php

namespace App\Filament\Resources;

use App\Filament\Resources\UserResource\Pages;
use App\Models\User;
use Filament\Forms;
use Filament\Forms\Form;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;

class UserResource extends Resource
{
    protected static ?string $model = User::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                Forms\Components\TextInput::make('first_name')
                    ->required()
                    ->maxLength(255),
                Forms\Components\TextInput::make('last_name')
                    ->required()
                    ->maxLength(255),
                Forms\Components\TextInput::make('email')
                    ->email()
                    ->required()
                    ->maxLength(255),
                Forms\Components\TextInput::make('country_code')
                    ->maxLength(255)
                    ->default('971')
                    ->readOnly(),
                Forms\Components\TextInput::make('phone_number')
                    ->tel()
                    ->maxLength(255),
//                Forms\Components\DateTimePicker::make('email_verified_at'),
//                Forms\Components\TextInput::make('password')
//                    ->password()
//                    ->required()
//                    ->maxLength(255),
                Forms\Components\FileUpload::make('image')
                    ->image()
                    ->directory('users'),
                Forms\Components\Hidden::make('company_role_id')
                    ->default(2)
                    ->afterStateHydrated(function ($set, $state, $record) {
                        if (!$state) {
                            $set('company_role_id', 2);
                        }
                    }),
                Forms\Components\TextInput::make('company_role')
                    ->default('Share Holder')
                    ->label('Company Role')
                    ->afterStateHydrated(function ($set, $state, $record) {
                        if (!$state) {
                            $set('company_role', 'Share Holder');
                        }
                    })
                    ->disabled(),
                Forms\Components\Select::make('companies')
                    ->label('Companies')
                    ->relationship('companies', 'name')
                    ->multiple()
                    ->preload(),
                Forms\Components\Hidden::make('full_name')
                    ->dehydrated(false),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                Tables\Columns\ImageColumn::make('image')->circular(),
                Tables\Columns\TextColumn::make('first_name')
                    ->searchable(),
                Tables\Columns\TextColumn::make('last_name')
                    ->searchable(),
                Tables\Columns\TextColumn::make('email')
                    ->searchable(),
                Tables\Columns\TextColumn::make('country_code')
                    ->searchable(),
                Tables\Columns\TextColumn::make('phone_number')
                    ->searchable(),
//                Tables\Columns\TextColumn::make('email_verified_at')
//                    ->dateTime()
//                    ->sortable(),
                Tables\Columns\TextColumn::make('created_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('updated_at')
                    ->dateTime()
                    ->sortable()
                    ->toggleable(isToggledHiddenByDefault: true),
                Tables\Columns\TextColumn::make('companyRole.name')
                    ->label('Company Role'),
            ])
            ->filters([
                Tables\Filters\TrashedFilter::make(),
            ])
            ->actions([
                Tables\Actions\ViewAction::make(),
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                    Tables\Actions\ForceDeleteBulkAction::make(),
                    Tables\Actions\RestoreBulkAction::make(),
                ]),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index' => Pages\ListUsers::route('/'),
            'create' => Pages\CreateUser::route('/create'),
            'view' => Pages\ViewUser::route('/{record}'),
            'edit' => Pages\EditUser::route('/{record}/edit'),
        ];
    }

    public static function getEloquentQuery(): Builder
    {
        return parent::getEloquentQuery()
            ->withoutGlobalScopes([
                SoftDeletingScope::class,
            ]);
    }
}

问题截图

图片展示问题1
图片展示问题2

解决方法

  1. 指定磁盘配置
    在FileUpload组件中添加磁盘指定(假设使用public磁盘):

    Forms\Components\FileUpload::make('image')
        ->image()
        ->directory('users')
        ->disk('public');
    
  2. 创建存储软链接
    运行Laravel命令建立存储目录的访问链接:

    php artisan storage:link
    
  3. 自定义查看页图片展示
    默认查看页可能无法自动预览图片,可自定义查看页添加ImageEntry组件:

    // 在UserResource的getPages方法中修改view路由
    'view' => Pages\ViewUser::route('/{record}')
        ->mutateFormUsing(function (Form $form, $record) {
            return $form->schema([
                // 保留原有其他字段...
                Forms\Components\ImageEntry::make('image')
                    ->disk('public'),
                // 保留原有其他字段...
            ]);
        }),
    

内容的提问来源于stack exchange,提问作者karim harakeh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 00:07:25