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.
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>
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.
Follow the documentation to install the laravel-filemanager.
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(); }); });
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) }
That’s it, Laraberg image upload with laravel-filemanager is done here.
Today we are going to learn about managing multiple PHP versions on ubuntu with xampp.…
Let's understand about how to use coding to improve your website's SEO. In today’s computerized…
Let's understand the most important linux commands for web developers. Linux, as an open-source and…
Today we are going to discuss top 75+ Laravel interview questions asked by top MNCs.Laravel,…
Today we will discuss about the Mailtrap integration with laravel 10 .Sending and receiving emails…
Today we are going to integrate FCM (Firebase Cloud Messaging) push notifications with ionic application.Firebase…