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

ofureigbelos-9707055's avatar

Filament File Upload Form Returns [object Object] in View

Hello everyone, so i am having difficulty with returning multiple uploaded image url to my filament form FileUpload Input Field using the getUploadedFileUsing() method. See below for my code and further explanation.

           Forms\Components\Select::make('task_id')
                ->options(Task::all()->pluck('name', 'id'))
                ->required()
                ->disabled()
                ->reactive(),

            Forms\Components\FileUpload::make('evidence')
                ->label('View Submitted Evidence')
                ->disk('do')
                ->multiple()
                ->disabled()
                ->getUploadedFileUsing(function ($state){
                    
                    if ($state && is_array($state)) {
                        //debug return value for $state: dd($state);
                        $file = [];
                        foreach ($state as $url) {
                            if($url['url']){
                                $file[] = $url['url'];
                            } 
                        }
                        //debug return value for files dd($file);
                        return $file;
                    }
                    return [];
                })
                ->afterStateHydrated(function (callable $set, $get){
                    $task_id = Task::find($get('task_id'));
                    $evidence = $task_id->evidence;
                    if ($task_id && $evidence) {
                        $decodedUrl = json_decode($evidence, true);

                        if (isset($decodedUrl['urls']) && is_array($decodedUrl['urls'])) {
                            $formattedEvidence = collect($decodedUrl['urls'])->map( function ($item){
                                return [
                                    'url' => $item['url'],
                                    'name' => basename($item['url']),
                                ];
                            })->toArray();
                            
                            $set('evidence', $formattedEvidence);
                            Log::info('Formatted Evidence Urls', $set('evidence', $formattedEvidence));
                        
                        }else{

                            $set('evidence', []);
                            Log::warning('Evidence not found for ID:', ['task_id' => $task_id]);
                        }
                    }else{
                        $set('evidence', []);
                    }
                }),

The intent is to fetch the url data stored on the 'evidence' column of the Task table which are inserted as json data from the frontend using standard Laravel save(). Note i am using Filament form to manage the backend where i getting this issue. See below for the Dump and Die Debugs

						dd($state) returns 

						array:2 [
						0 => array:2 [
							"url" =>"https://adeptigo.lon1.digitaloceanspaces.com/learner_files/Ofure%20Igbelos/Task%201/DSvWUD98ZdracOS791lZXvZhRyRgYnIJ4w8aBA47.jpg"
								"name" => "DSvWUD98ZdracOS791lZXvZhRyRgYnIJ4w8aBA47.jpg"
							]
							1 => array:2 [
								"url" =>								"https://adeptigo.lon1.digitaloceanspaces.com/learner_files/Ofure%20Igbelos/Task%201/r86u2OP0fpiysPqoh5BmL9S7XQSedSTQ9tzZ24kO.jpg"
								"name" => "r86u2OP0fpiysPqoh5BmL9S7XQSedSTQ9tzZ24kO.jpg"
							 ]
							]

								While dd($file); returns;

								array:2 [
								0 =>									"https://adeptigo.lon1.digitaloceanspaces.com/learner_files/Ofure%20Igbelos/Task%201/DSvWUD98ZdracOS791lZXvZhRyRgYnIJ4w8aBA47.jpg"
								1 =>"https://adeptigo.lon1.digitaloceanspaces.com/learner_files/Ofure%20Igbelos/Task%201/r86u2OP0fpiysPqoh5BmL9S7XQSedSTQ9tzZ24kO.jpg"]

Now this appears on the filament form view as [object Object] rather than the actual files.

Also, a different approach has been to pass the the $url directly to a $file variable like this;

				foreach ($state as $url) {
				      if($url['url']){
                            $file = $url;
                       } 
                 }
                 //dd($file);
                 return $file;

This works but only returns the last item in the array and duplicates the image on view twice. See below for the Dump and Die returned for $file in this approach

    array:2 [
    "url" => "https://adeptigo.lon1.digitaloceanspaces.com/learner_files/Ofure%20Igbelos/Task%201/r86u2OP0fpiysPqoh5BmL9S7XQSedSTQ9tzZ24kO.jpg"
  "name" => "r86u2OP0fpiysPqoh5BmL9S7XQSedSTQ9tzZ24kO.jpg"
   ]
0 likes
4 replies
jlrdw's avatar

Looks like you need a foreach loop: $file[0], $file[1], etc

Something like:

foreach ($files as $file) {
// rest of code
1 like
ofureigbelos-9707055's avatar

@jlrdw Hi, please could you help with a sample code as i believe i have tried looping through the $file variable and still get an [object Object] returned. See below:

			->getUploadedFileUsing(function ($state){
			if ($state && is_array($state)) {
			//dd($state);
			$file = [];
			foreach ($state as $url) {
			$file[]= $url;
			foreach($file as $files){
			dd($files);
			}
			}
			return $files;
			}
			return [];})

This returns only the last item and duplicates it on the view

see dump and die return dd($files);

						array:2 [
						"url" =>"https://adeptigo.lon1.digitaloceanspaces.com/learner_files/Ofure%20Igbelos/Task%201/DSvWUD98ZdracOS791lZXvZhRyRgYnIJ4w8aBA47.jpg"
								"name" => "DSvWUD98ZdracOS791lZXvZhRyRgYnIJ4w8aBA47.jpg"
								]
jlrdw's avatar

@ofureigbelos-9707055 try like:

for ($i = 0; $i < count($files); $i++) {
    $files[$i]  // do whats needed here.
    

Just example not knowing all your code. Each file has an index like 0,1,2, etc.

But you have $file as $files? It's usually the plural first. But a foreach should also work.

1 like
tisuchi's avatar

@ofureigbelos-9707055 I would suggest to tweak your getUploadedFileUsing a bit. Try this:


 ->getUploadedFileUsing(function ($state) {
        if ($state && is_array($state)) {
            $files = [];

            foreach ($state as $fileData) {
                if (isset($fileData['url']) && isset($fileData['name'])) {
                    // Each file should be an associative array with 'url' and 'name'
                    $files[] = [
                        'url' => $fileData['url'],
                        'name' => $fileData['name'],
                    ];
                }
            }

            return $files; // Return an array of file data objects
        }

        return [];
    })
1 like

Please or to participate in this conversation.