Laravel Summernote Image Upload Example

By Hardik Savani July 23, 2021 Category : Laravel


This post will give you example of laravel summernote image upload. step by step explain upload image summernote laravel. you can understand a concept of image upload in summernote editor laravel. if you want to see example of laravel image upload in summernote then you are a right place.

As we know, summernote provide image upload by default. but when you upload image in summernote editor then they convert image into base64 string image and send as content. that content if we store in database then latter it can be issue with database size. so best way is when you upload image then it should store image in our public or storage folder.

here i will give you simple example of image will upload on folder. you can also use this example in laravel 6, laravel 7 and laravel 8 version.

Just let's follow bellow step and see preview as bellow:


Step 1: Create Laravel Project

first of all we need to get fresh Laravel 8 version application using bellow command, So open your terminal OR command prompt and run bellow command:

composer create-project laravel/laravel blog

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;

class CreatePostsTable 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 "upload" 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();


$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= "/upload/" . 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 Summernote Editor Image Upload Example</title>

<link href="[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">

<link href="[email protected]/dist/summernote.min.css" rel="stylesheet">



<div class="container mt-5">

<h1>Laravel Summernote Editor Image Upload Example -</h1>

<form method="post" action="{{ route('') }}" enctype="multipart/form-data">


<div class="form-group">


<input type="text" name="title" class="form-control" />


<div class="form-group">


<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="[email protected]/dist/js/bootstrap.min.js"></script>

<script src="[email protected]/dist/summernote.min.js"></script>

<script type="text/javascript">

$(document).ready(function () {


height: 450,






Now we are ready to run our example. so run bellow command so quick run:

php artisan serve

Now you can open bellow URL on your browser:


i hope it can help you...

Tags :