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

Jellevana's avatar

Can I import Images from public url within Laravel Filament Import

Hello everyone,

I am wondering if anyone has built an image import within the Filament Import action?

Basically what I want to do is: Upload images based on a public image URL, for example public url from google drive or an image stored on a publicly accessible server.

Currently I am using Laravel 11 in combination with Laravel Filament 3.2

I couldn't find anything related online or in the docs

I suppose I need to build something for example in the "fillRecordUsing" method myself. But need to make sure everything happens within the same structure as the FileUpload field from filament. But am not sure if that would be the correct approach. I feel like i'm missing something.

0 likes
1 reply
LaryAI's avatar
Level 58

To import images from a public URL within a Laravel Filament Import action, you can indeed use the fillRecordUsing method to handle the image download and storage. Here's a step-by-step solution to achieve this:

  1. Install Required Packages: Ensure you have the guzzlehttp/guzzle package installed for making HTTP requests. You can install it via Composer if it's not already installed:

    composer require guzzlehttp/guzzle
    
  2. Create the Import Action: Define your import action and use the fillRecordUsing method to handle the image download and storage.

  3. Download and Store the Image: Use Guzzle to download the image from the public URL and store it in your desired storage location.

Here's an example implementation:

use Filament\Actions\ImportAction;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use GuzzleHttp\Client;

class YourModelImport extends ImportAction
{
    protected function fillRecordUsing(array $data): array
    {
        $client = new Client();
        $imageUrl = $data['image_url']; // Assuming 'image_url' is the key in your import data

        // Download the image
        $response = $client->get($imageUrl);
        $imageContents = $response->getBody()->getContents();

        // Generate a unique filename
        $filename = Str::random(40) . '.' . pathinfo($imageUrl, PATHINFO_EXTENSION);

        // Store the image
        Storage::disk('public')->put('images/' . $filename, $imageContents);

        // Return the data with the stored image path
        return array_merge($data, [
            'image_path' => 'images/' . $filename,
        ]);
    }
}

Explanation:

  1. Guzzle Client: We use Guzzle to make an HTTP GET request to the public image URL.
  2. Download Image: The image is downloaded and its contents are retrieved.
  3. Generate Filename: A unique filename is generated using Laravel's Str::random method.
  4. Store Image: The image is stored in the public disk under the images directory.
  5. Return Data: The data array is returned with the added image_path key, which contains the path to the stored image.

Usage:

Make sure to use this import action in your Filament resource or wherever you handle the import logic.

use App\Filament\Resources\YourModelResource;
use App\Filament\Resources\YourModelResource\Pages\YourModelImport;

class YourModelResource extends Resource
{
    public static function getActions(): array
    {
        return [
            YourModelImport::make(),
        ];
    }
}

This should allow you to import images from public URLs and store them within your Laravel application using Filament's import actions.

Please or to participate in this conversation.