Laravel 11 CKEditor Image Upload Example

Websolutionstuff | May-20-2024 | Categories : Laravel

Hello, laravel web developers! In this article, we'll see how to image upload in CKeditor 5 in laravel 11. CKEditor is a WYSIWYG rich text editor. which enables writing content directly inside of web pages or online applications.

Its core code is written in JavaScript and it is developed by CKSource. CKEditor is available under open source. CKEditor is a modern, feature-rich JavaScript text editor with a clean UI and perfect UX. Easily customizable to any use case.

Laravel 11 CKEditor Image Upload

laravel 11 CKEditor image upload

 

Inserting images into content created with CKEditor 5 is a very common task. In a properly configured rich-text editor, there are several ways for the end user to insert images:

  • Pasting an image from the clipboard
  • Dragging a file from the file system
  • Selecting an image through a file system dialog
  • Selecting an image from a media management tool in your application
  • Pasting a URL to an image, either into the editor dialog or directly into the content.

 

Step 1: Install Laravel 11 Application

In this article, we'll install laravel 11 using the following composer command.

composer create-project laravel/laravel laravel_11_example

 

Step 2: Define Route

Then, we'll define the routes to the web.php file for displaying the text editor and uploading images.

routes/web.php

<?php
  
use Illuminate\Support\Facades\Route;
  
use App\Http\Controllers\CkeditorController;
   
/*
|--------------------------------------------------------------------------
| 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!
|
*/
    
Route::get('ckeditor', [CkeditorController::class, 'index']);
Route::post('ckeditor/upload', [CkeditorController::class, 'upload'])->name('ckeditor.upload');

 

Step 3: Create Controller

Next, we'll create a CkeditorController.php file using the following command.

php artisan make:controller CkeditorController

app/Http/Controllers/CkeditorController.php

<?php
    
namespace App\Http\Controllers;
    
use Illuminate\Http\Request;
use Illuminate\View\View;
use Illuminate\Http\JsonResponse;
    
class CkeditorController extends Controller
{
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function index(): View
    {
        return view('ckeditor');
    }
    
    /**
     * Write code on Method
     *
     * @return response()
     */
    public function upload(Request $request): JsonResponse
    {
        if ($request->hasFile('upload')) {
            $originName = $request->file('upload')->getClientOriginalName();
            $fileName = pathinfo($originName, PATHINFO_FILENAME);
            $extension = $request->file('upload')->getClientOriginalExtension();
            $fileName = $fileName . '_' . time() . '.' . $extension;
      
            $request->file('upload')->move(public_path('images'), $fileName);
      
            $url = asset('media/' . $fileName);
  
            return response()->json(['fileName' => $fileName, 'uploaded'=> 1, 'url' => $url]);
        }
    }
}

Note: Make sure you have created an images folder in your public directory because images will be stored in that folder.

 

Step 4: Create View File

Next, we'll create a ckeditor.blade.php file. So, add the following code to that file.

resources/views/ckeditor.blade.php

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 11 CKEditor Image Upload Example - Websolutionstufff</title>
    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">
    <style>
        .ck-editor__editable_inline {
            min-height: 300px;
        }
    </style>
</head>
<body>
    
<div class="container">
    <h1>Laravel 11 CKEditor Image Upload Example - Websolutionstuff</h1>
    <form>
        <div class="form-group">
            <strong>Title:</strong>
            <input type="text" name="title" class="form-control" placeholder="Title" value="{{ old('title') }}">
        </div><br>
        <div class="form-group">
            <strong>Description:</strong>
            <textarea name="editor" id="editor"></textarea>
        </div>
        <div class="form-group">
            <button class="btn btn-success" type="submit">Submit</button>
        </div>
    </form>
</div>
<script src="https://cdn.ckeditor.com/ckeditor5/37.1.0/classic/ckeditor.js"></script>
<script>
    ClassicEditor.create( document.querySelector( '#editor' ),{
        ckfinder: {
            uploadUrl: '{{route('ckeditor.upload').'?_token='.csrf_token()}}',
        }
    })
    .catch( error => {
            
    });
</script>
     
</body>
</html>

Output:

laravel 11 image upload ckeditor 5
 


You might also like:

Recommended Post
Featured Post
How to Convert Datetime into Milliseconds in PHP
How to Convert Datetime into M...

Converting DateTime into milliseconds in PHP can be useful for various applications, such as timestamping events or meas...

Read More

Mar-15-2024

How To Add Bootstrap 5 Modal In Laravel 10
How To Add Bootstrap 5 Modal I...

In this article, we will see how to add the bootstrap 5 modal in laravel 10. Here, we will learn about the bootstra...

Read More

Apr-12-2023

How to Integrate Razorpay Payment Gateway in Laravel
How to Integrate Razorpay Paym...

In this article, we will see the most important and exciting toping about how to integrate razorpay payment gateway in l...

Read More

Jan-06-2021

How To Install php-zip Extension In Ubuntu
How To Install php-zip Extensi...

In this article, I will guide you through the process of installing the php-zip extension on Ubuntu. The php-zip extensi...

Read More

Jul-14-2023