0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

File uploading with CKEditor on Laravel-Admin

Posted at

Install CKEditor on Laravel-admin

At first, install CKEditor on laravel-admin follow the steps below.
https://github.com/laravel-admin-extensions/ckeditor

Adding file upload function on CKEditor

config/admin.php

...
        'ckeditor' => [
            'enable' => true,
            'config' => [
                'filebrowserImageUploadUrl' => '/admin/fileupload', // Add here
            ]   
        ]   
...

Another options here.

app/Admin/routes.php

use Illuminate\Routing\Router;
 
Admin::routes();
 
Route::group([
    'prefix'        => config('admin.route.prefix'),
    'namespace'     => config('admin.route.namespace'),
    'middleware'    => config('admin.route.middleware'),
    'as'            => config('admin.route.prefix') . '.',
], function (Router $router) {
 
    $router->get('/', 'HomeController@index')->name('home');
    $router->post('/fileupload', 'FileuploadController@index'); // Add here
    ... 
});

app/Http/Middleware/VerifyCsrfToken.php

<?php
 
namespace App\Http\Middleware;
 
use Illuminate\Foundation\Http\Middleware\VerifyCsrfToken as Middleware;
 
class VerifyCsrfToken extends Middleware
{
    /** 
     * The URIs that should be excluded from CSRF verification.
     *
     * @var array
     */
    protected $except = [ 
        '/admin/fileupload', // Add here
    ];  
}

app/Admin/Controllers/FileuploadController.php

<?php
 
namespace App\Admin\Controllers;
 
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
 
class FileUploadController extends Controller
{
 
    public function index(Request $request)
    {   
        $image     = $request->file('upload');
        $name      = substr(str_shuffle('1234567890abcdefghijklmnopqrstuvwxyz'), 0, 12);
        $extension = $image->getClientOriginalExtension();
        $newname   = $name . '.' . $extension;

        // you can change third variable as filesystem you want.
        $image->storeAs([ANY_DIRECTORY_YOUWANT], $newname, 'public');
        $url = [YOUR_PUBLIC_PATH] .$newname; 
 
        $param = [ 
            'uploaded' => 1,
            'fileName' => $newname,
            'url'      => $url
        ];  
 
        return response()->json($param, 200); 
    }   
}

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?