Laravel 8 Image Upload Example

Websolutionstuff | Oct-06-2020 | Categories : Laravel PHP jQuery

In this article, we will see the laravel 8 image upload example. Image or file upload is the most common task in web development. So here, we will show you how to upload images in laravel 8. Here we will see laravel 8 upload images to a public folder. So, we will create two routes, one for the get method and the second for the post method, and also we are creating a basic form with file input. So you have to simply select the image and then it will upload in the "images" directory of the public folder.

So, let's see the laravel 8 image upload and display.

Image Upload In Laravel 8 With Preview

Step 1: Add Routes

First of all, we need to install laravel 8 if you have already then add routes in the routes/web.php file shown below

Route::get('upload/image', 'App\Http\Controllers\UserController@ImageUpload');
Route::post('upload/image/store', 'App\Http\Controllers\UserController@ImageUploadStore');

 

Step 2: Create Controller

In this step, we will create new UserController and we will add two method ImageUpload() and ImageUploadStore(). here the first method will handle the get method other one for the post.

app/Http/Controllers/UserController .php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;

class UserController extends Controller
{
    public function ImageUpload()
    {
    	return view('index');	
    }

    public function ImageUploadStore(Request $request)
    {
    	 $request->validate([
            'image' => 'required|image|mimes:jpeg,png,jpg|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 3: Create Blade File

In the blade file, we will create a basic form with an upload button. So, add the below code.

<html>
<head>
    <title>Laravel 8 Image Upload Example - websolutionstuff.com</title>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
    <div class="container">
        <h2 style="margin-top: 30px;">Laravel 8 Image Upload Example - websolutionstuff.com</h2>
        <div class="panel-body"> 
            <div class="col-md-8">    
            @if ($message = Session::get('success'))
            <div class="alert alert-success alert-block">
                <button type="button" class="close" data-dismiss="alert">×</button>
                    <strong>{{ $message }}</strong>
            </div>
            <img src="{{asset('images')}}/{{ Session::get('image') }}" width="300" height="300">
            @endif
        
            @if (count($errors) > 0)
                <div class="alert alert-danger">
                    <strong>Whoops!</strong> There were some problems with your input.
                    <ul>
                        @foreach ($errors->all() as $error)
                            <li>{{ $error }}</li>
                        @endforeach
                    </ul>
                </div>
            @endif
            
            <form action="{{ url('upload/image/store') }}" method="POST" enctype="multipart/form-data">
            @csrf
                <div class="row"> <br>   
                    <div class="col-md-6">
                        <input type="file" name="image" class="form-control">
                    </div>     
                    <div class="col-md-6">
                        <button type="submit" class="btn btn-success">Upload</button>
                    </div>     
                </div>
            </form>
            </div>    
        </div>
    </div>
</body>  
</html>

 

 

Output:

laravel 8 image upload example

 


You might also like:

Recommended Post
Featured Post
How to Add and Delete Rows Dynamically using jQuery
How to Add and Delete Rows Dyn...

In this article, we will see how to add and delete rows dynamically using jquery. Also, we will see without&nb...

Read More

Jan-02-2021

Laravel 9 Many To Many Polymorphic Relationship
Laravel 9 Many To Many Polymor...

In this article, we will see laravel 9 many to many polymorphic relationship. many to many polymorphic relationship more...

Read More

Apr-06-2022

Send Mail Example In Laravel 8
Send Mail Example In Laravel 8

In this article, we will see send mail in laravel 8. here we will see how to send mail in laravel 8. Emai...

Read More

Oct-16-2020

How To Install php-zip Extension In Ubuntu
How To Install php-zip Extensi...

In this article, I will guide you through the process of installing the php-zip extension on Ubuntu. The php-zip extensi...

Read More

Jul-14-2023