Laravel-Media-Library

Why Media Library?

Medialibrary is a Laravel5.5+ package that can associate all sorts of files with the Eloquent models. And it provides a simple, fluent API to work with.

Features of Media Library

• It can directly handle your uploads
• It can store some large files on another filesystem because the storage of the files is handled by Laravel’s Filesystem, so you can plug in any compatible filesystem.
• The package can generate derived images such as thumbnails for images, video’s and pdf’s.

Requirements for Media Library v7/

• PHP version should be 7.1+
• Laravel version requires 5.5.0+
• MySQL required 5.7 or higher
If you have an older version of PHP and Laravel, then you should use a media library with a lower version.
In this article, I have used the spatie/laravel-medialibrary package. Now, I will show you how to install the media library package in Laravel with the few steps. And with this, I will show you a simple example of this package.

1. Installation & Setup of Media Library

Step1: Install Laravel

First install the Laravel project using composer commands:
composer create-project –prefer-dist laravel/laravel LaravelMedia

Step2: Install Medialibrary Package

After this, go to your Laravel project directory and run this composer command:
composer require “spatie/laravel-medialibrary:^7.0.0”
This command will install medialibray package in your Laravel project.

Step3: Database configuration

First, create the database for this media library package. Open phpMyAdmin and create database like this:

Next we need to configure database with your Laravel project. So, open .env file and configure like this:

Step4: Publish & Run migration

Next, we need to publish and run the migration for the medialibrary. Run this artisan command:
php artisan vendor:publish –provider=”Spatie\MediaLibrary\MediaLibraryServiceProvider” –tag=”migrations”

This command will create media table for your media library. You can see the migration table in your project database/migrations/create_media_table.php directory like this:

Next, run the artisan migrate command: php artisan migrate

Step5: Publish configuration file

Next, we need to publish the configuration file for media library. So run the command in you terminal:
php artisan vendor:publish –provider=”Spatie\MediaLibrary\MediaLibraryServiceProvider” –tag=”config”

This command will create configuration file for media library and create medialibrary.php file at your config directory.
Default content of this config/medialibrary.php file:

2. Basic Usage:

Preparing your model
To associate media with a model, the model must implement the following interface and trait like this:

For example, I have use this trait in our default User.php model like this:

Make Basic Layouts and Routes for media library
Create authentication for your Laravel project. Run the make:auth artisan command in your terminal:
php artisan make:auth

Next, open default resources/views/layouts/app.blade.php file and add write code for adding an extra profile link.

You will get output like this:

Create view file for media library:
Create profile.blade.php view file under resources/views/ directory and write a line of code for your profile views file.

Define route for views and controller in routes/web.php file like this:

After this register the user and navigate the URL 127.0.0.1:8000/profile in your browser, you will get output like this:

Associating files:
For associating the file, create a controller and define media function in your controller.
Create resource controller using make artisan command: php artisan make:controller AvatarController –r. This will create a controller file at app/Http/Controller/ directory.

Next, open AvatarController.php file and associate the file in different function like this:
Write code in index function like this:

Write code in store function like this:

Defining Media Collections:
A media collection can be more than just a name to group files. By defining a media collection in your model you can add certain behavior collections.
To get started with media collections add a function called registerMediaCollections to your prepared model like User.php model. Inside that function you can use addMediaCollection to start media collection.
Open User.php model and define media collection like this:

Defining Conversion
You should add a method called registerMediaConversions to your model. In that model you can define the media conversion. Here is an example.
Open User.php model and define the media conversion:

3 Results

Open browser and navigate the URL: 127.0.0.1:8000

Register the user first: 127.0.0.1:8000/register

Next, click to profile and upload images, you will get output like this:

Note:
• To learn more about this media library package, visit this link:
https://docs.spatie.be/laravel-medialibrary/v7/introduction/.
• Source code for this project is available on GitHub: https://github.com/digitalcrm/LaravelMedia.

Leave a comment

Your email address will not be published. Required fields are marked *