Do you want to install CKEditor in Laravel? CKEditor is a WYSIWYG(what you see is what you get) HTML editor that allows you to write rich text formats. In this article, I show you how to install and use CKEditor in Laravel. I will also explain how one can upload images in CKEditor.
This tutorial will be focused on adding CKEditor 4 in Laravel. For the integration of CKEditor 5, refer to our article on how to use CKEditor 5 in Laravel.
Why Need to Use CKEditor?
A rich text editor is necessary to write rich content for your pages or articles. On the website, we need to have different elements like images, paragraphs, links, etc. All these different elements can be easily added using the CKEditor.
HTML provides a textarea
element for writing description. But, it comes with limitations. In textarea
, it’s not easy and user-friendly to write HTML elements like p, div, img, etc.
To overcome such limitations, you can use CKEditor which itself is a rich text editor. It allows us to insert HTML elements between content. Below is the screenshot of how the CKEditor appears on a page.
Install CKEditor in Laravel
There are 2 ways to install CKEditor in Laravel – via CDN or using the CKEditor’s package. Both resources are available on the CKEditor’s download page.
The CDN link is //cdn.ckeditor.com/4.21.0/standard/ckeditor.js
. If you choose CDN, you don’t need to download anything from the CKEditor’s website.
To install CKEditor without CDN, download the package(Standard Package recommended) on your machine. Next, create a folder ckeditor
under the public
directory of your Laravel project. And inside this ckeditor
folder copy below files and folders from the downloaded package.
How to Use CKEditor in Laravel
After completing the previous step, you are ready with your CKEditor’s package. Now let’s see how to use the CKEditor.
Let’s say you have a Text area that should get replaced by CKEditor. To do so I am adding an id editor
to the textarea
.
<textarea id="editor" name="editor"></textarea>
Next, you need to include ckeditor.js
file and write a JavaScript code that replaces this textarea
with CKEditor.
<script src="{{ asset('ckeditor/ckeditor.js') }}"></script>
<script>
CKEDITOR.replace( 'editor' );
</script>
Above JavaScript code convert your textarea into the CKEditor. In order to use CDN the code would be as follow:
<script src="//cdn.ckeditor.com/4.21.0/standard/ckeditor.js"></script>
<script>
CKEDITOR.replace( 'editor' );
</script>
Your CKEditor is ready. You can now add your rich content to the editor and use it on your website. To get the content of CKEditor use the statement below.
$content = $request->input('editor');
And to pre-fill the CKEditor with the content, just pass the value to the Text area as usual.
<textarea id="editor" name="editor">Your content here</textarea>
Upload and Insert Image in CKEditor
CKEditor by default does not give the option to upload the image. Enabling this feature requires writing a piece of code. It needs to add a Laravel route, a script for image upload, and some JavaScript. First, to provide the image upload option you need to call CKEditor in the following way.
<script>
CKEDITOR.replace( 'editor', {
filebrowserUploadUrl: "{{route('upload', ['_token' => csrf_token() ])}}",
filebrowserUploadMethod: 'form'
});
</script>
Here for the key filebrowserUploadUrl
I passed the route URL and csrf token. Let’s define the route upload
as follows.
Route::post('ckeditor/image_upload', 'EditorController@upload')->name('upload');
Head over to the terminal and create this EditorController.
php artisan make:controller EditorController
Reload the page. Click on CKEditor’s image icon and you will see the image upload option as shown below.
Now to insert the chosen image in CKEditor, you have to upload the image on the server and send back an image URL. For storing an image on a server, I create a symbolic link(symlink) of a storage
folder. Run the command below to create a symlink:
php artisan storage:link
The route upload
map with the upload method of EditorController
. Define upload
method in the EditorController
as follows:
<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class EditorController extends Controller
{
public function upload(Request $request)
{
if($request->hasFile('upload')) {
//get filename with extension
$filenamewithextension = $request->file('upload')->getClientOriginalName();
//get filename without extension
$filename = pathinfo($filenamewithextension, PATHINFO_FILENAME);
//get file extension
$extension = $request->file('upload')->getClientOriginalExtension();
//filename to store
$filenametostore = $filename.'_'.time().'.'.$extension;
//Upload File
$request->file('upload')->storeAs('public/uploads', $filenametostore);
$CKEditorFuncNum = $request->input('CKEditorFuncNum');
$url = asset('storage/uploads/'.$filenametostore);
$msg = 'Image successfully uploaded';
$re = "<script>window.parent.CKEDITOR.tools.callFunction($CKEditorFuncNum, '$url', '$msg')</script>";
// Render HTML output
@header('Content-type: text/html; charset=utf-8');
echo $re;
}
}
}
That’s it! Now try to upload the image and you will get the image added successfully to the CKEditor.
I hope you understand how to install and use CKEditor in Laravel. Please share your thoughts and suggestions in the comment below.
Related Articles
- How to Install and Use TinyMCE Editor in Laravel
- How to Install and Use Trix Editor in Laravel
- PayPal Payment Gateway Integration in Laravel
If you liked this article, then please subscribe to our YouTube Channel for video tutorials.
how to add on laravel
This article is about CKEditor in Laravel itself.
sorry i put facebook url, cause i haven’t website.
i want ask how if error when i send it to the server in image upload and error is HTTP error occurred during file upload (error status: 405). in header response the message error is “The POST method is not supported for this route. Supported methods: GET, HEAD.”
hi, image with success uploading, but not showing
Thank you! It works!
same problem as Deniz Setyawan works fine in localhost but images not showing in production server
Works fine for me
Instead of getting the uploaded image in my blade view, i get this::
Image
Any help?
A link to the image with html tags
thanks, it works in localhost, but after i’ve uploaded to my hosting, it doesn’t work, can you help me?
See if any error in the console or in response.
https://1.bp.blogspot.com/-sFqJkdqT1I0/XsM1an6pfRI/AAAAAAAAEHQ/raZ78UH2g0k4nWmgPidOW69-ZWBRz2ipACK4BGAsYHg/s320/1122.JPG . the textarea is fine, the problem only on file upload
https://1.bp.blogspot.com/-qm4ky-uGRpc/XsM7aVqdh8I/AAAAAAAAEHs/TaV-59YmTFEBzb2R4E6bw0CtrCAtmo0RQCK4BGAsYHg/aaa.JPG . i think it’s from the controller right?, but i still confused where should i directs it. public_html?
Did you create a symbolic link using the command:
already done that
This is very nice
I try this one on windows, then it functions well but when i put it on linux server the image upload does not function.
can some one help me?
Check if you have given write permission for image folder.
Good system, but I need code highlighting for programming languages. Please tell me how to implement code highlighting in ckeditor, for example php, css, sql, html. P.S. everything is simple: 1) add a plugin to ckeditor. 2) add the language selection button to the ckeditor panel. 3) Then add everything to the vendor. http: / / ckeditor. com / ke4/addon/prism
Thank you
hi there,
I have followed and do it every thing according to instruction on this website (https://artisansweb.net/install-use-ckeditor-laravel/). But i am not getting the editor but only textarea.
I did not understand why. Please help me.
regards!
Is there any JavaScript error showing in the console?
Thank you very much
UnSharp left the ckeditor/laravel project and now its not working with latest laravel version.
Thanks for pointing out. I updated the article and now it is working with the latest Laravel version.
hi
how can we enable and disabled ckeditor with checked box in laravel?
Excellent tutorial
I save the images to the Storage folder and they are physically deposited there, already run in CMD $ php artisan storage: link, the folder pointing to storage was created, but even so when I enter the URL of the uploaded image it marks me 404 error, I’m sure the route is correct
the same problem for me .. route me not show … I think the file .httacss is made the problem
Thank You.
Not Found
The requested URL /blog/ckeditor/image_upload was not found on this server.
Apache Server at localhost Port 80
http://127.0.0.1:8000 say Image source URL is missing
I got his headache i cant understand the bug i tried a lot can you find it.
you copy URL in this post ?
very nice, thanks
How could I do If I want to install the full pagkage (not a standard package)?
I don’t have their full package so never tested with it. But I believe you need to copy files, folders from your full package to public/vendor/unisharp/laravel-ckeditor directory.
Great tutorial, thank you so much…
I want upload image from computer with CKeditor, so how I do, cant you guide me?
Hanah
Image upload feature does not come default in CKEditor. It needs customization. We’ll try to publish an article on this topic soon.
Hanah, I updated the article and explains how to upload and insert image in CKEditor.
How to add images through CKEditor?
I updated the article and explains how to upload and insert image in CKEditor.
Many thanks.
You could link to instructions that show how to apply “on change” on the object?
hello
editor worked and save data in database , but when i want show text on front page . show html tag in page
like ” some ting ”
don’t hidden html tag .
how i can fix that problem ?
Use the strip_tags function.
Instead using {{}} use {!! !!}
Will work fine.
I found this solution here: https://github.com/UniSharp/laravel-ckeditor/issues/26
i use ck editor for in laravel but i have a problem is aries . all the tag are display in the view hoe to hide such display tab in my view page
The Editor works okay but data is not insert
Did you forget to give a name for textarea? Using a name you can get it at the backend and store it in a database.
The Editor works okay but is saving the HTML tags as well to the Database. Is there a way to trim of the HTML tags when rendering the Database response in the blade view?
Use the strip_tags function.
CKeditor disappears why?
Im quite sure ive done right, saad.
i keep on getting this error
Class ‘Unisharp\Ckeditor\ServiceProvider::class’ not found
how can this be fixed?
Unisharp\Ckeditor\ServiceProvider::class
Put this on config\app in providers array.
i used it with laravel collective and it dont work source file was included CKeditor id was replaced
hi
how to ful pakeg ckeditor to laravel