Laravel 9 Summernote Image Upload Tutorial

Mar 15, 2022 . Admin

Hi dev,

Today, I am explaining the travel 9 summernote image upload tutorial. I would like to show you laravel 9 image upload in the Summernote editor example. in this example, you will learn to add Summernote editor in laravel 9 application. you will learn how to upload an image file with Summernote editor in laravel 9 applications. If you are searching how to image file upload using Summernote editor in laravel 9 application. step by step explain upload image Summernote laravel 9.

If you use simply textarea and you require to make more tools like bold, italic, or image upload, Then you have to choose Summernote plugin.

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 initialized. We typically load the Plugin Script after loading the Summernote Script. Most scripts are added in the head area of the typical HTML page.

Step 1: Download Laravel

Let us begin the tutorial by installing a new laravel application. if you have already created the project, then skip following step.

composer create-project laravel/laravel example-app
Step 2: Create Post Table and Model

in first step, we need create new migration for adding "posts" table:

php artisan make:migration create_posts_table
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
     * Run the migrations.
     * @return void
    public function up()
        Schema::create('posts', function (Blueprint $table) {
     * Reverse the migrations.
     * @return void
    public function down()

now let's run migration command:

php artisan migrate

now, just create post model and add code as like bellow:

namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Post extends Model
    use HasFactory;
    protected $fillable = [
Step 3: Create Route

In this is step we need to create some routes for listing posts and creating post.

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\PostController;
| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
Step 4: Create Controller

in this step, in this file we write image upload code, image will upload on "uploads" folder in public directory. we need to create PostController and add following code on that file:

namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
class PostController extends Controller
     * Write code on Method
     * @return response()
    public function create()
        return view('postsCreate');
     * Write code on Method
     * @return response()
    public function store(Request $request)
        $this->validate($request, [
             'title' => 'required',
             'body' => 'required'
       $content = $request->body;
       $dom = new \DomDocument();
       $dom->loadHtml($content, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
       $imageFile = $dom->getElementsByTagName('img');
       foreach($imageFile as $item => $image){
           $data = $image->getAttribute('src');
           list($type, $data) = explode(';', $data);
           list(, $data)      = explode(',', $data);
           $imgeData = base64_decode($data);
           $image_name= "/uploads/" . time().$item.'.png';
           $path = public_path() . $image_name;
           file_put_contents($path, $imgeData);
           $image->setAttribute('src', $image_name);
       $content = $dom->saveHTML();
       $post = Post::create([
            'title' => $request->title,
            'body' => $content
Step 5: Create Blade Files

here, we need to create blade files for index and create. so let's create one by one files:

<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>Laravel 9 Summernote Image Upload Tutorial -</title>
    <link href="" rel="stylesheet">
    <link rel="stylesheet" href="" />
    <div class="container mt-4">
        <h3 class="text-center">Laravel 9 Summernote Image Upload Tutorial -</h3>
        <form method="post" action="{{ route('') }}" enctype="multipart/form-data" class="mt-5">
            <div class="form-group">
                <input type="text" name="title" class="form-control" />
            <div class="form-group my-3">
                <textarea id="summernote" name="body"></textarea>
            <div class="form-group text-center">
                <button type="submit" class="btn btn-success btn-block">Publish</button>
    <script src=""></script>
    <script src=""></script>
    <script src=""></script>
    <script type="text/javascript">
        $(document).ready(function () {
                height: 300,
Run Laravel App:

All steps have been done, now you have to type the given command and hit enter to run the laravel app:

php artisan serve

Now, you have to open web browser, type the given URL and view the app output:


I hope it can help you...

#Laravel 9