Basic usage of laravel-medialibrary

I'm assuming that you have very basic knowledge of Laravel Framework.

Installing the Library

First of all, we have to install the library. Just copy and paste one by one command below.

composer require spatie/laravel-medialibrary:^7.0.0

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="migrations"

php artisan migrate

php artisan vendor:publish --provider="Spatie\MediaLibrary\MediaLibraryServiceProvider" --tag="config"
1
2
3
4
5
6
7

Setup

.env file

You need to setup the .env files APP_URL field according to the server.

We need to make a model for the to upload image. The images will be corespondent to this model. With that model we need Controller file and also the migration file.

This command will generate the necessary file needed for our purpose.

php artisan make:model News -a
1

This will generate the News model, NewsController, news migration file and the NewsFactory.

Model file

Now, we have to configure the model file (News.php) to associate with this library.

<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMedia;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
class News extends Model implements HasMedia
{
    use HasMediaTrait;
}
1
2
3
4
5
6
7
8
9

Migration file

Now configure the migration file (create_news_table.php) to have some fields for the News.

<?php
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateNewsTable extends Migration
{
    /**
    * Run the migrations.
    *
    * @return void
    */
    publicfunctionup()
    {
        Schema::create('news',function(Blueprint$table){
            $table->increments('id');
            $table->string('name');
            $table->text('description');
            $table->timestamps();
       });
    }
    /**
    * Reverse the migrations.
    *
    * @return void
    */
    publicfunctiondown()
    {
        Schema::dropIfExists('news');
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30

Routing file

Create route for the News(web.php).

Route::get('/setNews', 'NewsController@create');

Route::get('/getNews', 'NewsController@index');
1
2
3

Factory file

Create a news using factory so that you have a row in the database(NewsFactory.php).

<?php
use Faker\Generator as Faker;
$factory->define(App\News::class, function (Faker $faker) {
    return[
        'name'=>$faker->colorName,
        'description'=>$faker->realText(),
    ];
});
1
2
3
4
5
6
7
8

Seeder file

Seed the News table.(NewsSeeder.php)

<?php
use App\News;
use Illuminate\Database\Seeder;
class NewsSeeder extends Seeder
{
    /**
    * Run the database seeds.
    *
    * @return void
    */
    publicfunctionrun()
    {
         factory(News::class,20)->create();
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

Creating Blade file

Create the blade file for displaying the images you upload.(news.blade.php)

<metacharset="UTF-8">
<metaname="viewport"content="width=device-width, initial-scale=1.0">
<metahttp-equiv="X-UA-Compatible"content="ie=edge">

@foreach($images as $image)
    {{ $image }}
@endforeach
1
2
3
4
5
6
7

WARNING

Create the storage link using this command

php artisan storage:link
1

Now you can upload image by hitting the /setNews route and to see the uploaded image go to the /getNews route.