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

Watheq's avatar
Level 14

Can't Seed images with Spatie laravel media library?

I have this error "Could not add {$url}because it does not start with eitherhttp://orhttps://`` when I enter the command php artisan migrate:fresh --seed.

this is my LocationTableSeeder class

 public function run()
    {
        $locations = [
            'London',
            'Liverpool',
            'Manchester',
            'Birmingham',
        ];

        foreach ($locations as $location) {
            $slug = Str::slug($location);
            $locationObject =  Location::create(
                [
                    'name' => $location,
                    'slug' => $slug,
                ]
            );

            $locationObject->addMediaFromUrl(public_path('/images/locations/' . $slug . '.jpg'))->toMediaCollection('photo');
        }

I am using "spatie/laravel-medialibrary": "^9.7" and laravel 8

2 likes
4 replies
Watheq's avatar
Watheq
OP
Best Answer
Level 14

I have found the answer: it should be addMedia instead of addMediaFromUrl like this:

$locationObject->addMedia(public_path('/images/locations/' . $slug . '.jpg'))->toMediaCollection('photo');
3 likes
lmottasin's avatar

@Watheq this aslo deleted the photos from that directory after deleting

2 likes
jpedrera's avatar

@lmottasin try including this:

$yourModel->addMedia(...)
   ->preservingOriginal() // <= include this
   ->toMediaCollection(...);
2 likes

Please or to participate in this conversation.