Summernote WYSIWYG Editor is a JavaScript library that allows users to create WYSIWYG editors with many features on forms in Laravel 11.
Let’s start to create text editor on forms using summernote cdn:
Step 1: Create a Form
Create a form in your view file in resources/views/ directory:
<!DOCTYPE html>
<html lang="{{ str_replace('_', '-', app()->getLocale()) }}">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Laravel 11 Summernote Editor Image Upload Example - ItcodStuff.com</title>
</head>
<body>
<div class="container">
<div class="card mt-5">
<h3 class="card-header p-3">Laravel 11 Summernote Editor Image Upload Example - ItcodStuff.com</h3>
<div class="card-body">
<form method="post" action="{{ route('posts.store') }}" 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>Description</label>
<textarea id="summernote" name="body"></textarea>
</div>
<div class="form-group mt-2">
<button type="submit" class="btn btn-success btn-block">Publish</button>
</div>
</form>
</div>
</div>
</div>
</body>
</html>
Step 2: Include Summernote JS and CSS
Add the Summernote CSS and JS files to your view file:
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.20/summernote-bs5.min.css" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.20/summernote-bs5.min.js"></script>
Step 3: Set Up Summernote
Initialize the Summernote function to create a text editor with image upload feature:
<script type="text/javascript">
$(document).ready(function () {
$('#summernote').summernote({
height: 300,
});
});
</script>
Step 4: Create a Route for Image Upload
Create a route in your routes/web.php file to handle the image upload:
use App\Http\Controllers\PostController;
Route::get('/', function () {
return view('welcome');
});
Route::post('posts/store',[PostController::class,'store'])->name('posts.store');
Step 5: Implement Image Upload Logic
In your controller file, implement the upload method to handle the image upload:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Post;
use Illuminate\View\View;
use Illuminate\Http\RedirectResponse;
class PostController extends Controller
{
/**
* Write code on Method
*
* @return response()
*/
public function store(Request $request): RedirectResponse
{
$this->validate($request, [
'title' => 'required',
'body' => 'required'
]);
$post = Post::create([
'title' => $request->title,
'body' => $request->body
]);
return back()
->with('success','Post created successfully.');
}
}
Step 6: Update Model
Edit your model and update the following code into it:
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Casts\Attribute;
class Post extends Model
{
use HasFactory;
/**
* Write code on Method
*
* @return response()
*/
protected $fillable = [
'title', 'body'
];
/**
* Get the user's first name.
*/
protected function body(): Attribute
{
return Attribute::make(
set: fn (string $value) => $this->makeBodyContent($value),
);
}
/**
* Write code on Method
*
* @return response()
*/
public function makeBodyContent($content)
{
$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= "/images/" . time().$item.'.png';
$path = public_path() . $image_name;
file_put_contents($path, $imgeData);
$image->removeAttribute('src');
$image->setAttribute('src', $image_name);
}
return $dom->saveHTML();
}
}
Now you can test your laravel summernote image upload.