Table of contents

laravel image upload

In this post, I will share an example of how to upload an image on Laravel 8. When developing a Laravel application the functionality of uploading images is always there. Usually, we require our users to upload their profile photos for some verification purposes. Or a gallery module for your Laravel application. So in this example, I will show to you an example about Laravel image upload.


laravel image upload


Step 1: Laravel Installation

If you don't have a Laravel 8 install in your local just run the following command below:

composer create-project --prefer-dist laravel/laravel image-upload


Step 2: Create Controller

Next, we will create a controller for our Laravel image upload. Run the following artisan command:

php artisan make:controller ImageUploadController


Step 3: Create Routes

Next, we will add our routes. Navigate routes/web.php the add the following code.


use Illuminate\Support\Facades\Route;

| Web Routes
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!

Route::group(['namespace' => 'App\Http\Controllers'], function()
    Route::get('/image-upload', 'ImageUploadController@index')->name('image-upload.index');
    Route::post('/image-upload', 'ImageUploadController@upload')->name('');


Step 4: Setup Controller Methods

Now lets, create our method of our Laravel image upload controller.


namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\ImageUploadRequest;

class ImageUploadController extends Controller
    public function index() 
        return view('image-upload.index');

    public function upload(ImageUploadRequest $request) 
        $filename = time() . '.' . $request->image->extension();

        $request->image->move(public_path('images'), $filename);

        // save uploaded image filename here to your database

        return back()
            ->with('success','Image uploaded successfully.')
            ->with('image', $filename); 


Step 5: Create Laravel Image Upload Request

Next, we will add our validation request. Run the following command:

php artisan make:request ImageUploadRequest


Step 6: Add View

Next, we will add our view for our image upload index.blade.php. Navigate the directory resources/views/ then create an image-upload folder.


Then create file index.blade.php see below code:

<!DOCTYPE html>

        <meta charset="utf-8" />
        <meta name="csrf-token" content="{{ csrf_token() }}">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Laravel 8 Image Upload Example -</title>
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
        <script src=""></script>
        <script src=""></script>

        <div class="container mt-5">
            @if(Session::get('success', false))
              <?php $data = Session::get('success'); ?>
              @if (is_array($data))
                  @foreach ($data as $msg)
                      <div class="alert alert-success" role="alert">
                          <i class="fa fa-check"></i>
                          {{ $msg }}
                  <div class="alert alert-success" role="alert">
                      <i class="fa fa-check"></i>
                      {{ $data }}

              <img src="images/{{ Session::get('image') }}" width="200px">

            <form action="{{ route('') }}" method="POST" enctype="multipart/form-data">
                <div class="row">
                    <div class="col-md-6">
                        <input type="file" name="image" class="form-control">
                        @if ($errors->has('image'))
                            <span class="text-danger text-left">{{ $errors->first('image') }}</span>
                    <div class="col-md-6">
                        <button type="submit" class="btn btn-success">Upload</button>


Now test it. Run command below:

php artisan serve


Then hit this URL to your browser:


Now you have a simple functionality to upload images in Laravel 8. I hope it helps. Thank you for reading :)