In this tutorial you will learn about the Laravel 7 Summernote Image Upload Example and its application with practical example.
In this Laravel 7 Summernote Image Upload Example tutorial, I’ll show you how to use Summernote for Image Upload in laravel. In this tutorial you will learn to upload image using Summernote in laravel.
Laravel 7 Summernote Image Upload Example
- Step 1: Install Laravel App
- Step 2: Add Database Details
- Step 3: Create Migration and Model File
- Step 4: Add Routes
- Step 5: Create Controller
- Step 6: Create Blade File
- Step 7: Run Development Server
Step 1: Install Laravel 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: Add Database Details
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=Enter_Your_Database_Name DB_USERNAME=Enter_Your_Database_Username DB_PASSWORD=Enter_Your_Database_Password |
Step 3: Create Migration and Model File
Now, in this step we will create model and migration file for form. Please run the following command:
1 |
php artisan make:model Post -m |
Next, Navigate to database/migrations and open create_posts_table.php. Then update the following code into create_books_table.php file, as follow:
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 |
<?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreatePostsTable extends Migration { /** * Run the migrations. * * @return void */ public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->longText('description'); $table->timestamps(); }); } /** * Reverse the migrations. * * @return void */ public function down() { Schema::dropIfExists('posts'); } } |
Now, run the migration to create database table using following artisan command:
1 |
php artisan migrate |
Now, go to App directory and open Post.php model file. Then add the following code into Post.php model file, as follow:
app/Post.php
1 2 3 4 5 6 7 8 9 10 |
<?php namespace App; use Illuminate\Database\Eloquent\Model; class Post extends Model { protected $fillable = ['title','description']; } |
Step 4: Add Routes
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 |
Route::get('summernote-image-upload','PostController@index'); Route::post('post-summernote-image-upload','PostController@store'); |
Step 5: Create Controller
Now, lets create a controller named PostController using command given below –
1 |
php artisan make:controller PostController |
Then, Navigate to app/http/controllers and open PostController.php file. And update the following code into your PostController.php file, as follow:
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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
<?php namespace App\Http\Controllers; use Illuminate\Http\Request; use App\Post; class PostController extends Controller { /** * Display a listing of the resource. * * @return \Illuminate\Http\Response */ public function index() { return view('create'); } /** * Store a newly created resource in storage. * * @param \Illuminate\Http\Request $request * @return \Illuminate\Http\Response */ public function store(Request $request) { $this->validate($request, [ 'title' => 'required', 'description' => 'required' ]); $description = $request->description; $dom = new \DomDocument(); $dom->loadHtml($description, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD); $images = $dom->getElementsByTagName('img'); foreach($images as $k => $img){ $data = $img->getAttribute('src'); list($type, $data) = explode(';', $data); list($type, $data) = explode(',', $data); $data = base64_decode($data); $image_name= "/upload/" . time().$k.'.png'; $path = public_path() . $image_name; file_put_contents($path, $data); $img->removeAttribute('src'); $img->setAttribute('src', $image_name); } $description = $dom->saveHTML(); $summernote = new Post; $summernote->title = $request->title; $summernote->description = $description; $summernote->save(); echo "<h1>Title</h1>" , $Title; echo "<h2>Description</h2>" , $description; } } |
Step 6: Create Blade File
In this step, Navigate to resources/views folder. And create a blade views that named create.blade.php the file inside this folder. Then open create.blade.php file and update the following code into create.blade.php file, as follow:
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 |
<!DOCTYPE html> <html> <title>Laravel Summernote Image Upload Example Tutorial</title> <script type="text/javascript" src="https://code.jquery.com/jquery-1.9.1.min.js"></script> <link rel="stylesheet" href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" /> <script type="text/javascript" src="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/js/bootstrap.min.js"></script> <!-- include summernote css/js--> <link href="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.4/summernote.css" rel="stylesheet"> <script src="http://cdnjs.cloudflare.com/ajax/libs/summernote/0.8.4/summernote.js"></script> </head> <body> <div class="container"> <div class="row"> <div class="col-md-8 offset-2 mt-5"> <div class="card"> <div class="card-header bg-info"> <h6 class="text-white">Laravel Summernote Image Upload Example</h6> </div> <div class="card-body"> <form method="post" action="{{ url('post-summernote-image-upload') }}" enctype="multipart/form-data"> @csrf <div class="form-group"> <label>Title</label> <input type="text" name="title" class="form-control"/> </div> <div class="form-group"> <label><strong>Description :</strong></label> <textarea class="summernote" name="description"></textarea> </div> <div class="form-group text-center"> <button type="submit" class="btn btn-success btn-sm">Save</button> </div> </form> </div> </div> </div> </div> </div> <script type="text/javascript"> $(document).ready(function() { $('.summernote').summernote({ height: 300, }); }); </script> </body> </html> |
Step 7: Run 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://localhost:8000/summernote-image-upload |