Laravel Laravel 6 Laravel 7

Laravel 7 Ckeditor Image Upload Example

Pinterest LinkedIn Tumblr

Step 1: Install the laravel project using the command line.

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

Step 2: Open project in code and use vs code terminal.

cd laravel7Ckeditor

Step 3: find .env file in root directory setup database configuration.

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=database_name
DB_USERNAME=database_username
DB_PASSWORD=database_password

Step 4: Route

Open routes/web.php file.

Route::get('/', '[email protected]');
Route::post('ckeditor/upload', '[email protected]')->name('ckeditor.upload');

Step 5: Controller

Create CkeditorController Controller.

php artisan make:controller CkeditorController

Open app/Http/Controllers/CkeditorController.php file.

<?php
  
namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class CkeditorController extends Controller
{
    /**
     * success response method.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        return view('ckeditor');
    }
  
    /**
     * success response method.
     *
     * @return \Illuminate\Http\Response
     */
    public function upload(Request $request)
    {
        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);
   
            $CKEditorFuncNum = $request->input('CKEditorFuncNum');
            $url = asset('images/'.$fileName); 
            $msg = 'Image uploaded successfully'; 
            $response = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";
               
            @header('Content-type: text/html; charset=utf-8'); 
            echo $response;
        }
    }
}

Step 6: Create blade file

Create a new directory ckeditor at resources/views/ directory.
In resources/views/ directory create a new ckeditor.blade.php file.

<!DOCTYPE html>
<html>
<head>
    <title>Laravel 7 Ckeditor Image Upload Example</title>
    <script src="https://cdn.ckeditor.com/4.12.1/standard/ckeditor.js"></script>
</head>
<body>
  
<h1>Laravel 7 Ckeditor Image Upload Example</h1>
<textarea name="editor1"></textarea>
   
<script type="text/javascript">
    CKEDITOR.replace('editor1', {
        filebrowserUploadUrl: "{{route('ckeditor.upload', ['_token' => csrf_token() ])}}",
        filebrowserUploadMethod: 'form'
    });
</script>
   
</body>
</html>

Step 7: Run server

php artisan serve

Write A Comment

Open chat
Hey Visitor,

How can I help you?