in this tutorial you will see how to upload image with spatie mediaLibrary package.
you will also learn how to upload image,image edit,image delete.today you will see all in one place
I’m pretty sure many of you may have found yourself gotten into the situation where you do not edit,
or delete image with post
Laravel image Upload full crud with spatie mediaLibrary package
Step 1: Set Up Laravel Project
Step 2: Set Up Database Details in ENV
Step 3: Create Model and Migration
Step 4: Install laravel-medialibrary
Step 5: Set Up laravel-medialibrary
Step 6: Perform Crud Operations
Set Up Laravel Project
Installing a fresh new laravel application, so head over to the terminal, type the command, and create a new laravel app.
composer create-project --prefer-dist laravel/laravel laravel_image_crud
Now, You have to move to the project folder:
cd laravel_image_crud
Set Up Database Details in ENV
Now, you have to connect the laravel app to the database, hence open the .env configuration file and add the database credentials as suggested below.
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_user_name
DB_PASSWORD=database_password
Create Model and Migration
In the terminal screen, type the recommended command and execute it to generate model and migration files.
php artisan make:model Image -m
You need to add the $fillable array and add the table values app/Models/Image.php file.
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
use Spatie\MediaLibrary\HasMedia;
use Spatie\MediaLibrary\InteractsWithMedia;
class Image extends Model implements HasMedia
{
use HasFactory, InteractsWithMedia;
protected $fillable = [
'name'
];
}
Read aslo
Laravel php artisan inspire command
Laravel clear cache without using artisan command