Skip to main content
Premium Members Only
Join to unlock this tutorial and all of our courses.
Tutorial Premium Tutorial

Laravel Spatie Media Library: 8 Less-Known Features with Demos

May 11, 2023
22 min read

Premium Members Only

This advanced tutorial is available exclusively to Laravel Daily Premium members.

Premium membership includes:

Access to all premium tutorials
Video and Text Courses
Private Discord Channel

Comments & Discussion

JN
Jeff Norris ✓ Link copied!

Very impressive package which I have used successfully in the past. FWIW services such as cloudinary & cloudflare images are very compelling alternatives and I now recommend their use. (Cloudinary more expensive but more powerful ... cloudflare super basic & cheap.) I use filepond for uploading (via livewire), which allows for size reduction & HEIC conversion on the client prior to server upload, so practical e.g. to upload iPhone photos which do not otherwise require the gigantic resolution.

PA
Paul Albandoz ✓ Link copied!

Any recommendation to import a large amount of media related with models? (user photos for example)

PK
Povilas Korop ✓ Link copied!

Import from where? Describe the exact situation, please.

But generally, it's a good old foreach in most cases.

PA
Paul Albandoz ✓ Link copied!

From an existing repository (a local folder). I think I am describing the initial seeding of a project that you want to populate with users and their related photos (that you already have stored in a folder).

M
Modestas ✓ Link copied!

There's a few ways this can be done.

  1. Plain old Foreach, but that will quickly hit a timeout or something as it will depend on the users amount.
  2. Creating a job, that would create a user with X details and import Y image. Run a ton of those jobs via queue worker and it will import things.

In any case, you should have something like .csv file that already contains user information + their preferred image.

As for how, you can do it by:

$user->addMedia(\Storage::path('path_to_image'))->preservingOriginal()->toMediaCollection();

Just don't forget to preserve the original or make copies!