Upload Image in Laravel 6 Example from Scratch

How to upload image in Laravel 6 is the today's topic. Today, i will show you how to create simple image upload in laravel 6. I write article step by step about image upload in laravel. I also added validation with image upload in laravel.

Here i also validate image where user can only upload jpeg,png,jpg,gif type image. So doing it you can download a fresh laravel file or you can do it in your current running project.

In this example, we will create two routes one for get method and another for post method. we created simple form with file input.

So you have to simple select image and then it will upload in "images" directory of public folder. So you have to simple follow bellow step and get image upload in laravel 6 application.

laravel-6-image-upload

Step 1 : Install Laravel 6

To do it run bellow command in your project directory.

composer create-project --prefer-dist laravel/laravel imageUpload

Step 2: Create Route

In next step, we will add new two routes in web.php file. One route for generate form and another for post method So let's simply create both route as bellow listed:

Read more : How to upload image in Laravel using trait

routes/web.php

Route::get('imageupload', '[email protected]')->name('imageupload');
Route::post('imageupload', '[email protected]')->name('imageuploadpost');

Step 3: Create ImageUploadController

Now go to your ImageUploadController and paste those following code.

app/Http/Controllers/ImageUploadController.php

namespace App\Http\Controllers;
  
use Illuminate\Http\Request;
  
class ImageUploadController extends Controller
{
    public function imageUpload()
    {
        return view('imageUpload');
    }
  
    public function imageUploadPost(Request $request)
    {
        $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        ]);
  
        $imageName = time().'.'.$request->image->extension();  
   
        $request->image->move(public_path('images'), $imageName);
   
        return back()
            ->with('success','You have successfully upload image.')
            ->with('image',$imageName);
   
    }
}

Step 4: Create Blade File

At last step we need to create imageUpload.blade.php file and in this file we will create form with file input button. So copy bellow and put on that file.

resources/views/imageUpload.blade.php

Step 5: Create "images" Directory

in last step, we need to create new directory "images" with full permission, So let's create new folder on public folder. Now you can check it.

A web enthusiastic, self-motivated Full-Stack Web Developer from Dhaka, Bangladesh with experience in developing applications using JavaScript, Laravel & Wordpress specifically. Facebook Github Website