Laravel

Laraberg image upload with laravel-filemanager – Laravel 8.x?

Share your learning

Few days back I was looking for Laraberg image upload with laravel-filemanager but I didn’t find that much result. So, I decided to figure it out own and share with others.

If you are also a fan of WordPress Gutenberg Editor then you might be familiar with Laraberg.

Laraberg is a Laravel package to use wordpress gutenberg editor with Laravel Framework.

When I used it, I found one issue there, that image uploading was not that much easy. I mean it just returns the image path and we need to take care of uploading it to the server.

I have used the Laravel-filemanager package to upload the images to the server and manage the media gallery.

So, let’s start this tutorial.

Install the Laraberg

I have directly jumped to the Laraberg installation, and I believe that you are already familiar with the Laravel installation.

Install the laraberg via composer

composer require van-ons/laraberg

Run the below command to publish the service provider of laraberg

php artisan vendor:publish --provider="VanOns\Laraberg\LarabergServiceProvider"

To integrate the assets like css and js, Gutenberg uses react, react-dom and jquery. So, include these CDN or downloaded files on the page where you want to use Gutenberg editor.

<script src="https://unpkg.com/react@17.0.2/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@17.0.2/umd/react-dom.production.min.js"></script>
<link rel="stylesheet" href="{{asset('vendor/laraberg/css/laraberg.css')}}">
<script src="{{ asset('vendor/laraberg/js/laraberg.js') }}"></script>

It’s time to initialize the Laraberg Editor

In the blade file add the below textarea.

<textarea id="post-editor" name="post_html">
    @if(isset($post)) {!! $post->body !!} @endif
</textarea>

Then in the javascript call the laraberg and init it.

(function() {
                if (typeof Laraberg == 'undefined') {
                    console.warning('Laraberg not found!');
                    return;
                }

                Laraberg.init('post-editor', {
                    mediaUpload: mediaUploaded,
                    minHeight: '100vh',
                });
})


const mediaUploaded = ({
            filesList,
            onFileChange
        }) => {
 console.log(filesList);
}

As you can see, in the mediaUploaded arrow function we are getting the uploaded file. Now we need to upload this file to the server via laravel-filemanager.

Laravel-filemanager installation

Follow the documentation to install the laravel-filemanager.

Laravel-filemanager Routes

We have created the route to access laravel-filemanager as given in the documentation of Laravel-filemanager.

Route::middleware(['auth'])->group(function() {
   Route::get('/home', [HomeController::class, 'index'])->name('home');
 
   Route::group(['prefix' => 'filemanager'], function () {
       \UniSharp\LaravelFilemanager\Lfm::routes();
   });
});

JQuery Ajax to upload file to the Laravel-filemanager

If you still remember the above javascript arrow function which we have created to handle file uploading from Laraberg. It’s time to complete that function.

const mediaUploaded = ({
           filesList,
           onFileChange
       }) => {
           setTimeout(async () => {
 
               let formD = new FormData;
               Array.from(filesList).map(file => {
                   formD.append('upload', file);
               });
 
               const uploadedResponse = await $.ajax({
                   method: "POST",
                   url: `${APP_URL}/filemanager/upload`,
                   data: formD,
                   processData: false,
                   contentType: false,
                   success: function(response) {
                       console.log({
                           response
                       })
                       return response;
                   },
                   error: function(savePostErr) {
                       console.log({
                           savePostErr
                       })
                   }
               })
 
 
               const uploadedFiles = Array.from(filesList).map(file => {
 
                   return {
                       id: new Date().getTime(),
                       name: file.name,
                       url: uploadedResponse.url
                   }
               })
 
               console.log(uploadedFiles)
 
               onFileChange(uploadedFiles)
           }, 1000)
       }

The final output is below in the images.

That’s it, Laraberg image upload with laravel-filemanager is done here.

Satpal

Recent Posts

How to Switch PHP Versions in XAMPP Easily: Managing Multiple PHP Versions on Ubuntu

Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…

1 year ago

How to Use Coding to Improve Your Website’s SEO Ranking?

Let's understand about how to use coding to improve your website's SEO. In today’s computerized…

1 year ago

Most Important Linux Commands for Web Developers

Let's understand the most important linux commands for web developers. Linux, as an open-source and…

1 year ago

Top 75+ Laravel Interview Questions Asked by Top MNCs

Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…

1 year ago

Mailtrap Integration for Email Testing with Laravel 10

Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…

1 year ago

Firebase Cloud Messaging (FCM) with Ionic 6: Push Notifications

Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…

1 year ago