Laravel File Manager using Spatie Media Library
Hello Artisans, today we'll discuss how to integrate Spatie FIle Manager using spatie new media Library package. This package is useful to keep track of each and every piece of information we may need for an uploaded file/image. So, no more talk, let's see how we can integrate spatie file manager in our Laravel Application.
Note: Tested on Laravel 9.11
Table of Contents
- Install and configure Spatie Media Library
- Create and Setup Controller
- Setup Model
- Define Routes
- Create and Setup blade File
- Output
Install and configure Spatie Media Library
To install the spatie media library, fire the below command in your terminal
After successfully completing the installation, run the below command to publish the migration
After publishing the migrations, migrate the tables with the following command.
That's all you need to do to install the spatie media library package.
Create and Setup Controller
First of all, create a controller so that we can write our logics or query to show the result. So, fire the below commands in the terminal.
It'll create a controller under app\Http\Controllers called UserController.php. Open the file and replace with below codes.
Setup Model
Now we need to change the model a little bit in which we want to associate media with a model. The model must implement the following interface and trait:
So, in our case we want to associate media with our User.php model. So finally our model will be look like below.
Define Routes
Now we need to put the below routes in our web.php
Create and Setup blade File
Now we need to create blade files for viewing the list of users and from where we'll create a new user with images. So, create two files under resources\views named
- users.blade.php
- create.blade.php
Now open the files and replace them with the following corresponding file codes.
Output
First of all, note that we need to fire the below commands to see the images
And after that, we need to fix our App URL in our .env file like below.
Or if you use any custom host/server then define that link.
And finally, we're ready with our setup. It's time to check our output. Now go to http://127.0.0.1:8000/users, If everything goes well you'll find the below output.
That's it for today. Hope you'll enjoy this tutorial. You can also download this tutorial Thanks for reading.🙂