📅  最后修改于: 2023-12-03 15:02:39.203000             🧑  作者: Mango
Laravel-MediaLibrary is a PHP package that provides a simple and flexible way to manage your application's media assets. It allows you to easily upload, retrieve, and manipulate various types of media, such as images, videos, and audio files.
Some of the key features of Laravel-MediaLibrary include:
You can install Laravel-MediaLibrary via Composer by adding the package to your composer.json
file and running the composer update
command:
composer require spatie/laravel-medialibrary
Next, you'll need to publish the package's configuration file and migration files:
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"
php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"
Finally, you'll need to run the package's database migrations:
php artisan migrate
Using Laravel-MediaLibrary is easy. First, you'll need to create a new media collection for each type of media you want to manage:
use Spatie\MediaLibrary\Models\Media;
class Post extends Model
{
use HasMediaTrait;
public function registerMediaCollections()
{
$this->addMediaCollection('images')
->registerMediaConversions(function (Media $media) {
$this->addMediaConversion('thumb')
->width(100)
->height(100);
});
}
}
Once you've created your media collections, you can easily add media to them:
$post = Post::find(1);
$post->addMediaFromUrl('https://example.com/image.jpg')->toMediaCollection('images');
You can then retrieve the media for a given model like so:
$media = $post->getMedia('images');
Overall, Laravel-MediaLibrary is a great choice for developers who need to manage their application's media assets in a flexible and modular way.