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
Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.
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
I have found the answer: it should be addMedia instead of addMediaFromUrl like this:
$locationObject->addMedia(public_path('/images/locations/' . $slug . '.jpg'))->toMediaCollection('photo');
Please or to participate in this conversation.