Laravel 8 Summernote Image Upload Tutorial

Laravel 8 Summernote Image Upload Tutorial

Laravel 8 summernote image upload tutorial example; Through this tutorial, you will learn how to upload image file with summernote editor in laravel 8 applications.

Adding a Plugin to Summernote is as easy as adding Summernote to the page you want Summernote to appear in. Some Plugins may also dynamically add styles to the DOM when initialised. We typically load the Plugin Script after loading the Summernote Script. Most scripts are added in the head area of the typical HTML page.

If you are searching how to image file upload using sumernote editor in laravel 8 application. So this laravel 8 summernote editor with image upload tutorial will help you to upload images with summernote editor in laravel 8 app.

How to Upload Image in Laravel 8 using Summernote Editor

  • Step 1 – Install Laravel 8 App
  • Step 2 – Connecting App to Database
  • Step 3 – Create Migration and Model File
  • Step 4 – Add Routes
  • Step 5 – Create Controller
  • Step 6 – Create Blade File
  • Step 7 – Run Development Server
  • Step 8 – Test This App

Step 1 – Install Laravel 8 App

In this step, Execute the following command on terminal to install or download laravel fresh application setup for implementing image upload with summernote editor in laravel 8 app:

composer create-project --prefer-dist laravel/laravel blog

Step 2 – Connecting App to Database

In this step, Navigate to your project root directory and open the “.env” file. Then add database details into .evn file, as follow:

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=Enter_Your_Database_Name
DB_USERNAME=Enter_Your_Database_Username
DB_PASSWORD=Enter_Your_Database_Password

Step 3 – Create Migration and Model File

In this step, Execute the following command on terminal to create a migration for post table and post Model in laravel 8 app. So run the following command on command prompt:

cd blog

php artisan make:model Post -m

Next, Navigate to database/migrations and open create_posts_table.php. Then update the following code into create_books_table.php file, as follow:

<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreatePostsTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('posts', function (Blueprint $table) {
            $table->id();
            $table->string('title');
            $table->longText('description');
            $table->timestamps();
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('posts');
    }
}

Then, run the following command on command prompt:

php artisan migrate

This command will create tables in your database.

After that, Navigate to App directory and open Post.php model file. Then add the following code into Post.php model file, as follow:

app/Post.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Post extends Model
{
    use HasFactory;

}

Step 4: Add Routes

In this step, Navigate to the routes folder and open web.php file. Then add the following routes into web.php file, as follow:

use App\Http\Controllers\PostController;

Route::get('summernote-image-upload', [PostController::class, 'index']);
Route::post('post-summernote-image-upload', [PostController::class, 'store']);

Step 5 – Create Controller

In this step, Execute the following command on terminal to create one controller file name PostController.php. So open your terminal and run the following command to create PostController file, as follow:

php artisan make:controller PostController

Then, Navigate to app/http/controllers and open PostController.php file. And update the following code into your PostController.php file, as follow:

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Models\Post;

class PostController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
   public function store(Request $request)
   {

       $this->validate($request, [

             'title' => 'required',

             'description' => 'required'

        ]);

   
       $description = $request->description;

       $dom = new \DomDocument();

       $dom->loadHtml($description, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);    

       $images = $dom->getElementsByTagName('img');

       foreach($images as $k => $img){


           $data = $img->getAttribute('src');

           list($type, $data) = explode(';', $data);

           list(, $data)      = explode(',', $data);

           $data = base64_decode($data);

           $image_name= "/upload/" . time().$k.'.png';

           $path = public_path() . $image_name;

           file_put_contents($path, $data);

           $img->removeAttribute('src');

           $img->setAttribute('src', $image_name);

        }


       $description = $dom->saveHTML();

       $summernote = new Post;

       $summernote->title = $request->title;

       $summernote->description = $description;

       $summernote->save();



   echo "<h1>Title</h1>" , $Title;

   echo "<h2>Description</h2>" , $description;

   }

}

Step 6 – Create Blade File

In this step, Navigate to resources/views folder. And create 1 blade views that named create.blade.php the file inside this folder.

Then open create.blade.php file and update the following code into create.blade.php file, as follow:

<!DOCTYPE html>
<html>
<title>Laravel 8 Summernote Image Upload Example Tutorial - tutsmake.com</title>
    <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script> 

    <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" />

    <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script>

    <!-- include summernote css/js-->
    <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.4/summernote.css" rel="stylesheet">

    <script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.4/summernote.js"></script>

</head>
<body>
    <div class="container">
        <div class="row">
            <div class="col-md-8 offset-2 mt-5">
                <div class="card">
                    <div class="card-header bg-info">
                        <h6 class="text-white">Laravel Summernote Image Upload Example</h6>
                    </div>
                    <div class="card-body">
                        <form method="post" action="{{ url('post-summernote-image-upload') }}" enctype="multipart/form-data">
                            @csrf
                            <div class="form-group">
                                <label>Title</label>
                                <input type="text" name="title" class="form-control"/>
                            </div>  
                            <div class="form-group">
                                <label><strong>Description :</strong></label>
                                <textarea class="summernote" name="description"></textarea>
                            </div>
                            <div class="form-group text-center">
                                <button type="submit" class="btn btn-success btn-sm">Save</button>
                            </div>
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script type="text/javascript">
        $(document).ready(function() {
         $('.summernote').summernote({
               height: 300,
          });
       });
    </script>
</body>
</html>

Step 7 – Run Development Server

Now, execute the following command on terminal to start development server:

php artisan serve

Step 8 – Test This App

Open your browser and hit the following URL on it:

http://localhost:8000/summernote-image-upload 

Conclusion

Laravel 8 summernote editor image upload example, you have learned how to upload image with summernote editor in laravel apps.

Recommended Laravel Tutorials

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Node JS, Express JS, Vue JS, Angular JS, React Js, MySQL, MongoDB, REST APIs, Windows, Xampp, Linux, Ubuntu, Amazon AWS, Composer, SEO, WordPress, SSL and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *