Every two weeks I send out a newsletter containing lots of interesting stuff for the modern PHP developer. You can expect quick tips, links to interesting tutorials, opinions and packages. Want to learn the cool stuff? Then sign up now!

Laravel medialibrary hits v4

Today we tagged a new major version of laravel-medialibrary. In case you’re not familiar with this package here’s a quick rundown of what it can do.

The package can associate all sorts of files with Eloquent models. It provides a simple, fluent API to work with. Here’s a quick example:

$newsItem = News::find(1);

Want to store some large files on another filesystem? No problem:

$newsItem->addMedia($smallFile)->toCollectionOnDisk('downloads', 'local');
$newsItem->addMedia($bigFile)->toCollectionOnDisk('downloads', 's3');

The package can also generate derived images such as thumbnails for images and pdf’s. For this to work you have to prepare your model a bit:

use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;

class NewsItem extends Model implements HasMediaConversions
    use HasMediaTrait;

    public function registerMediaConversions()
             ->setManipulations(['w' => 368, 'h' => 232])

Once that’s out the way you can to this to retrieve a thumbnail.


If you’re already using v3 of the package you’ll notice that there hasn’t changed much. Upgrading should be fairly painless. V4 is not a feature rich major version. The biggest changes are that Glide 1.0.0 is used behind the scenes to create thumbnails, and some internal functions were refactored.

Thanks to a lot of quality contributions from our users this is easily one of our best packages. If you like this one, take a look at the other ones we’ve previously made.

Freek Van der Herten is a partner and developer at Spatie, an Antwerp based company that specializes in creating web apps with Laravel. After hours he writes about modern PHP and Laravel on this blog. When not coding he’s probably rehearsing with his kraut rock band. He loves waffles and butterflies.
  • Big fan or your packages.. I will be using this next 🙂