DEV Community

Cover image for Laravel 8 image Upload crud using spatie mediaLibrary package
saim
saim

Posted on • Updated on • Originally published at larainfo.com

Laravel 8 image Upload crud using spatie mediaLibrary package

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

Top comments (1)

Collapse
 
sloan profile image
Info Comment hidden by post author - thread only accessible via permalink
Sloan the DEV Moderator

Hi there, we encourage authors to share their entire posts here on DEV, rather than mostly pointing to an external link. Doing so helps ensure that readers don’t have to jump around to too many different pages, and it helps focus the conversation right here in the comments section.

If you choose to do so, you also have the option to add a canonical URL directly to your post.

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