In this tutorial you will learn about the How to Install Ckeditor in Laravel 8 and its application with practical example.
In this How to Install Ckeditor in Laravel 8 tutorial I will show you how to install and use CKEditor in laravel. In this tutorial, you will learn to install CKEditor in laravel using the command line. In this article I will guide you through how to install and upload images and files using CKEditor in laravel.
While creating forms we come to situation where we may text editor field so that we can insert and save html and other text into database. The CKEditor editor is feature rich wysiwyg html editor allows us to integrate text editor fields in html form.
How to Install Ckeditor in Laravel 8
In this step by step tutorial I will demonstrate you how to install and use CKEditor editor in laravel 8 application. Please follow the instruction given below:
- Step 1: Install Laravel 8 App
- Step 2: Connecting App to Database
- Step 3: Create Post Model & Migration
- Step 4: Add Fillable Property in Model
- Step 5: Make Route
- Step 6: Create Controller
- Step 7: Create Blade Views File
- Step 8: Start Development Server
Step 1: Install Laravel 8 App
First of all we need to create a fresh laravel project, download and install Laravel 8 using the below command
1 |
composer create-project --prefer-dist laravel/laravel Blog |
Step 2: Connecting App to Database
Now, lets create a MySQL database and connect it with laravel application. After creating database we need to set database credential in application’s .env file.
1 2 3 4 5 6 |
DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=database-name DB_USERNAME=database-user-name DB_PASSWORD=database-password |
Step 3: Create Post Model & Migration
Now, in this step we will create model and migration file. Please run the following command:
1 |
php artisan make:model Post -m |
Now, open create_posts_table.php file inside /database/migrations/ directory. And the update the function up() with following code:
1 2 3 4 5 6 7 8 9 |
public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->text('body'); $table->timestamps(); }); } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Step 4: Add Fillable Property in Model
In this step, add the fillable property in Post model, which is located inside app/models directory:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php namespace App\Models; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; class Post extends Model { use HasFactory; protected $fillable = [ "title", "body" ]; } |
Step 5: Make Route
After this, we need to define routes in “routes/web.php” file. Lets open “routes/web.php” file and add the following routes in it.
routes/web.php
1 2 3 4 5 6 7 |
<?php use App\Http\Controllers\PostController; use Illuminate\Support\Facades\Route; Route::get('posts', [PostController::class, "index"]); Route::get("create", [PostController::class, "create"]); Route::post('store', [PostController::class, "store"]); |
Step 6: Create Controller
Now, lets create a controller named PostController using command given below –
1 |
php artisan make:controller PostController |
Once the above command executed, it will create a controller file PostController.php in app/Http/Controllers/ directory. Open the PostController.php file and put the following code in it.
app/Http/Controllers/PostController.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Models\Post; class PostController extends Controller { public function index() { $posts = Post::orderBy("id", "desc")->paginate(5); return view("posts", compact("posts")); } // Create Post public function create() { return view("create"); } public function store(Request $request) { $post = [ "title" => $request->title, "body" => $request->body ]; $post = Post::create($post); return back()->with("success", "Post has been created"); } } |
Step 7: Create Blade Views File
In this step we will create blade view file. Go to resource/views direcotry and create two files name posts.blade.php and create.blade.php file. Create a blade view file name posts.blade.php and put the following code into it:
posts.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
<!doctype html> <html lang="en"> <head> <title> Laravel 8 Posts List For CKEditor Example </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> <style> table td p { word-break: break-all; } </style> </head> <body> <div class="container mt-4"> <div class="row"> <div class="col-xl-8"> <h3 class="text-right"> Laravel 8 CKEditor Integration Example</h3> </div> <div class="col-xl-4 text-right"> <a href="{{url('create')}}" class="btn btn-primary"> Add Post </a> </div> </div> @if(count($posts) > 0) <div class="table-responsive mt-4"> <table class="table table-striped"> <thead> <tr> <th> Id </th> <th style="width:30%;"> Title </th> <th> Decription </th> </tr> </thead> <tbody> @foreach($posts as $post) <tr> <td> {{ $post->id }} </td> <td> {{ $post->title }} </td> <td> {!! html_entity_decode($post->body) !!} </td> </tr> @endforeach </tbody> </table> </div> {{ $posts->links() }} @endif </div> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> </body> </html> |
Now, create a blade view file name create.blade.php and put the following code into it:
create.blade.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
<!doctype html> <html lang="en"> <head> <title> Laravel 8 Install CKEditor Example </title> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous"> {{-- CKEditor CDN --}} <script src="https://cdn.ckeditor.com/ckeditor5/23.0.0/classic/ckeditor.js"></script> </head> <body> <div class="container mt-5"> <div class="row"> <div class="col-xl-12 text-right"> <a href="{{ url('posts') }}" class="btn btn-danger"> Back </a> </div> </div> <form action="{{url('store')}}" method="POST"> @csrf <div class="row"> <div class="col-xl-8 col-lg-8 col-sm-12 col-12 m-auto"> @if(Session::has('success')) <div class="alert alert-success alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> {{ Session::get('success') }} </div> @elseif(Session::has('failed')) <div class="alert alert-danger alert-dismissible"> <button type="button" class="close" data-dismiss="alert">×</button> {{ Session::get('failed') }} </div> @endif <div class="card shadow"> <div class="card-header"> <h4 class="card-title"> Laravel 8 Install CKEditor Example Tutorial </h4> </div> <div class="card-body"> <div class="form-group"> <label> Title </label> <input type="text" class="form-control" name="title" placeholder="Enter the Title"> </div> <div class="form-group"> <label> Body </label> <textarea class="form-control" id="body" placeholder="Enter the Description" name="body"></textarea> </div> </div> <div class="card-footer"> <button type="submit" class="btn btn-success"> Save </button> </div> </div> </div> </div> </form> </div> <script> ClassicEditor .create( document.querySelector( '#body' ) ) .catch( error => { console.error( error ); } ); </script> <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js" integrity="sha384-UO2eT0CpHqdSJQ6hJty5KVphtPhzWj9WO1clHTMGa3JDZwrnQq4sF86dIHNDz0W1" crossorigin="anonymous"></script> <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" crossorigin="anonymous"></script> </body> </html> |
Note that, Don’t forget to add the following cdn file to your blade view file:
1 |
<script src="https://cdn.ckeditor.com/ckeditor5/23.0.0/classic/ckeditor.js"></script> |
Step 8: Start Development Server
Now we are ready to run our example so lets start the development server using following artisan command –
1 |
php artisan serve |
Now, open the following URL in browser to see the output –
1 |
http://127.0.0.1:8000/create |