laravel-medialibrary Documentation

Associate files with Eloquent models

6,134 stars PHP PHP 1 file ยท ~1,568 tokens #eloquent#hacktoberfest#images#laravel#media#php#upload
RAW Doc

File: README.md

text
</a>

Associate files with Eloquent models



This package can associate all sorts of files with Eloquent models. It provides a
simple API to work with. To learn all about it, head over to the extensive documentation.

Here are a few short examples of what you can do:

php
$newsItem = News::find(1);
$newsItem->addMedia($pathToFile)->toMediaCollection('images');

It can handle your uploads directly:

php
$newsItem->addMedia($request->file('image'))->toMediaCollection('images');

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

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

The storage of the files is handled by Laravel's Filesystem,
so you can use any filesystem you like. Additionally, the package can create image manipulations
on images and pdfs that have been added in the media library.

Spatie is a webdesign agency in Antwerp, Belgium. You'll find an overview of all our open source projects on our website.

Support us

We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.

We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.

Documentation

You'll find the documentation on https://spatie.be/docs/laravel-medialibrary.

Find yourself stuck using the package? Found a bug? Do you have general questions or suggestions for improving the media library? Feel free to create an issue on GitHub, we'll try to address it as soon as possible.

If you've found a bug regarding security please mail [email protected] instead of using the issue tracker.

Testing

You can run the tests with:

bash
./vendor/bin/pest

You can run the Github actions locally with act. To run the tests locally, run:

text
act -j run-tests

To run tests for a specific PHP/Laravel version, run:

text
act -j run-tests --matrix php:8.3 --matrix laravel:"11.*" --matrix dependency-version:prefer-stable

Available matrix options are available in the workflow file.

Upgrading

Please see UPGRADING for details.

Changelog

Please see CHANGELOG for more information what has changed recently.

Contributing

Please see CONTRIBUTING for details.

Security

If you discover any security related issues, please email [email protected] instead of using the issue tracker.

Credits

A big thank you to Nicolas Beauvais for helping out with the issues on this repo.

Special thanks to Caneco for the original logo.

Alternatives

License

The MIT License (MIT). Please see License File for more information.


File: docs/introduction.md


title: Introduction weight: 1

This package can associate all sorts of files with Eloquent models. It provides a simple, fluent API to work with. The Pro version of the package offers Blade, Vue and React components to handle uploads to the media library and to administer the content of a medialibrary collection.

Here are some quick code examples:

php
$yourModel = YourModel::find(1);
$yourModel->addMedia($pathToFile)->toMediaCollection('images');

It can also directly handle your uploads:

php
$yourModel->addMediaFromRequest('image')->toMediaCollection('images');

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

php
$yourModel->addMedia($smallFile)->toMediaCollection('downloads', 'local');
$yourModel->addMedia($bigFile)->toMediaCollection('downloads', 's3');

The storage of the files is handled by Laravel's Filesystem, so you can plug in any compatible filesystem.

The package can also generate derived images such as thumbnails for images, videos and PDFs. Once you've set up your model, they're easily accessible:

php
$yourModel->getMedia('images')->first()->getUrl('thumb');

Are you a visual learner?

We've recorded a video course on how to use this package. It's the best way to get started using media library

We have badges!

[](https://github.com/spatie/laravel-medialibrary/releases) [](https://github.com/spatie/laravel-medialibrary/blob/master/LICENSE.md) [](https://travis-ci.org/spatie/laravel-medialibrary) [](https://scrutinizer-ci.com/g/spatie/laravel-medialibrary) [](https://packagist.org/packages/spatie/laravel-medialibrary)