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.
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.
First install the Laravel project using composer commands:
composer create-project –prefer-dist laravel/laravel LaravelMedia
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.
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:
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
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:
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:
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:
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:
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:
• To learn more about this media library package, visit this link:
• Source code for this project is available on GitHub: https://github.com/digitalcrm/LaravelMedia.