Laravel Image Upload with Spatie's Media Library Example
https://www.itsolutionstuff.com/post/laravel-image-upload-with-spaties-media-library-exampleexample.html
Laravel Image Upload with Spatie's Media Library Example
By Hardik Savani July 22, 2021 Category : LaravelPlayUnmuteLoaded: 1.17%Fullscreen
Hello,
I am going to show you example of laravel image upload with spatie media library. We will use laravel spatie media library tutorial. let’s discuss about laravel spatie media library. you'll learn laravel image upload spatie media library. Alright, let’s dive into the steps.
In this example we will do image upload using spatie/laravel-medialibrary composer package. Spatie Media Library provide easily image uploading with laravel eloquent model. using this package you can easily store image, get image, generate thumbnail image. you can use this example with laravel 6, laravel 7, laravel 8 and laravel 9 version.
Here, we will create posts table and we will add images of each post using spatie/laravel-medialibrary library and listing of posts with image.
Just let's follow bellow step and see preview as bellow:
Preview:

Step 1: Create Laravel Project
first of all we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command:
Step 2: Install spatie/laravel-medialibrary Package
in first step, we need install spatie/laravel-medialibrary composer package so let's use bellow command to install:
after installing successfully, we need to run following command to create migration for "media" table:
now let's run migration command:
Read Also: Laravel - Image Upload and Resize Example using Intervention Image Package
Step 3: Create Post Table and Model
in first step, we need create new migration for adding "posts" table:
database/migrations/2021_07_13_140744_create_posts_table.php
now let's run migration command:
now, just create post model and add code as like bellow:
app/Models/Post.php
Step 4: Create Route
In this is step we need to create some routes for listing posts and creating post.
routes/web.php
Step 5: Create Controller
in this step, we need to create PostController and add following code on that file:
app/Http/Controllers/PostController.php
Step 6: Create Blade Files
here, we need to create blade files for index and create. so let's create one by one files:
resources/views/posts/index.blade.php
resources/views/posts/create.blade.php
Now we need to do following configuration. you need to link storage folder to public by using following command:
then make sure your .env url path should be correct:
.env
Now we are ready to run our example. so run bellow command so quick run:
Now you can open bellow URL on your browser:
Read Also: How to Image Upload in Laravel Vapor?
you can get more method about Spatie Media Library.
i hope it can help you...
Last updated
Was this helpful?