How to upload image in Laravel in the best way?

Lets go through a Laravel 8 upload image example. This example will help you upload image to database in Laravel 8. Lets create a basic example of Laravel 8 image upload with preview.

We will create two routes, where one works for the get, while another for the post method, and a simple form with file input. So you have to simply select image and then it will upload in “images” directory of public folder of your Laravel project.

Step 1: Install Laravel & Create New Project

The first step is to make sure you work with Laravel 8 and create a new project with it.

composer create-project --prefer-dist laravel/upload_image blog

Step 2: Create new Routes

Next, add new two routes in routes/web.php file. One to generate input form and another for the post method.

Route::get('image-upload', [ UploadImageController ::class, 'uploadImage' ])->name('image.upload');
Route::post('image-upload', [ UploadImageController ::class, 'uploadImagePost' ])->name('');

Step 3: Create new Controller

In the third step we will create a new UploadImageController under the app/Http/Controllers directory, where we will define the two method uploadImage() and uploadImagePost(). So one method handles get method another one for post.

namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UploadImageController extends Controller
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function uploadImage()
        return view('uploadImage');
     * Display a listing of the resource.
     * @return \Illuminate\Http\Response
    public function uploadImagePost(Request $request)
            'image' => 'required|image|mimes:jpeg,png,jpg,gif,svg|max:2048',
        $imageName = time().'.'.$request->image->extension();  
        $request->image->move(public_path('images'), $imageName);
        /* Store $imageName name in DATABASE from HERE */
        return back()
            ->with('success','You have successfully upload image.')

The uploadImage() method returns a view where a user can upload the image. Meanwhile, the uploadImagePost() method deals with the uploaded image from the view. It validates the image type, checks the mimes types and its size. Then moves the image to the public_path specified for the project. A success message returns with the image path, if the process goes smoothly without errors.

Step 4: Create new blade file

The blade view that is returned in the uploadImage() method is coded as shown below.

<!DOCTYPE html>
    <title>laravel 8 upload  image example -</title>
    <link rel="stylesheet" href="">
<div class="container">
    <div class="panel panel-primary">
      <div class="panel-heading"><h2>laravel 8 upload image example -</h2></div>
      <div class="panel-body">
        @if ($message = Session::get('success'))
        <div class="alert alert-success alert-block">
            <button type="button" class="close" data-dismiss="alert">×</button>
                <strong>{{ $message }}</strong>
        <img src="images/{{ Session::get('image') }}">
        @if (count($errors) > 0)
            <div class="alert alert-danger">
                <strong>Oops!</strong> There were some problems with your input.
                    @foreach ($errors->all() as $error)
                        <li>{{ $error }}</li>
        <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">
                <div class="col-md-6">
                    <button type="submit" class="btn btn-success">Upload</button>

The blade file above views the upload image option to the user. In addition, it has two separate divs that pop up required messages. One for success and one for error. The error directive will help display any error messages.

You can store an image at two locations

The Storage Folder

$request->image->storeAs('images', $imageName);
// storage/app/images/file.png

The Public Folder

$request->image->move(public_path('images'), $imageName);
// public/images/file.png

The above code allows you to upload an image with the mimes type and under the maximum size mentioned with the required keyword inside the validate function.