📜  laravel-medialibrary packagist - PHP (1)

📅  最后修改于: 2023-12-03 15:02:39.203000             🧑  作者: Mango

Laravel-MediaLibrary Packagist - PHP

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.

Features

Some of the key features of Laravel-MediaLibrary include:

  • Support for multiple media collections
  • Ability to easily add and remove media to collections
  • Automatic generation of media conversion variants
  • Integration with Laravel's storage mechanisms
  • Support for dynamic file name generation
  • Built-in support for popular storage services like S3, Google Cloud Storage, and Azure Blob Storage
Installation

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
Usage

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.