DEV Community

loading...
Cover image for Laravel 8 image Upload crud using spatie mediaLibrary package

Laravel 8 image Upload crud using spatie mediaLibrary package

larainfo
Full Stack Developer | Laravel | Livewire | Vue Js Nodejs, tailwind css
Originally published at larainfo.com Updated on ・2 min read

in this tutorial you will see how to upload image with spatie mediaLibrary package.

Read Full Article...

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
Enter fullscreen mode Exit fullscreen mode

Now, You have to move to the project folder:

cd laravel_image_crud
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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'
    ];
}
Enter fullscreen mode Exit fullscreen mode

Read Full Article...

Read aslo

Laravel php artisan inspire command
Laravel clear cache without using artisan command

Discussion (1)

Some comments have been hidden by the post's author - find out more