Laravel 8 Image Upload with Spatie Media Library Tutorial

In this tutorial you will learn about the Laravel 8 Image Upload with Spatie Media Library Tutorial and its application with practical example.

In this Laravel 8 Image Upload with Spatie Media Library Tutorial, I’ll show you how to upload image with Spatie Media Library in laravel 8. In this laravel image upload tutorial you will learn to upload image into folder and then save it into database. In this article I will share example to upload image using Spatie Media Library in laravel 8. In this example we will be using Spatie Media Library to upload image in laravel application. Before saving image into database we will show preview of the images and then save it into directory. Before uploading the image we will display preview of the image. After successfully image upload into the database and folder we will display success message on the screen.

Laravel 8 Image Upload with Spatie Media Library Tutorial

In this step by step tutorial you will learn how to Upload image with Spatie Media Library Tutorial. Please follow the instruction given below:

  • Step 1: Create New Laravel Project.
  • Step 2: Database Connection.
  • Step 3: Define App Url.
  • Step 4: Add Spatie Medialibrary Package.
  • Step 5: Create Model and Migration.
  • Step 6: Formulate New Controller.
  • Step 7: Register Routes.
  • Step 8: Create Blade View Template.
  • Step 9: Evoke Development Server.

Create New Laravel Project

First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command

Database Connection

Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.

Add Spatie Media library Package

In this step, we will install Spatie Media library Package via the composer dependency manager. Use the following command to install Spatie Media library Package.

Now run the vendor publish command:

Create Model and Migration

Now, in this step we will create model and migration file. Please run the following command:

Once above command is executed there will be a migration file created inside database/migrations/ directory, just open migration file and update the function up() method as following:

Open app/Models/Blog.php insert the table value and add the Spatie media library modules.

Now, run the migration to create database table using following artisan command:

Create New Controller

Now, lets create a controller named BlogController using command given below –

Once the above command executed, it will create a controller file BlogController.php in app/Http/Controllers/ directory. Open the BlogController.php file and put the following code in it.

app/Http/Controllers/BlogController.php

Register Routes

After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.

routes/web.php

Create Blade View Template

Now, create and update app/resources/views/index.blade.php file.

Afetr that , create and insert the code in the app/resources/views/media.blade.php file.

Run Development Server

Now we are ready to run our example so lets start the development server using following artisan command –

Now, open the following URL in browser to see the output –

In this tutorial we have learn about the Laravel 8 Image Upload with Spatie Media Library Tutorial and its application with practical example. I hope you will like this tutorial.