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('image.upload.post');
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.
<?php
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)
{
$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);
/* Store $imageName name in DATABASE from HERE */
return back()
->with('success','You have successfully upload image.')
->with('image',$imageName);
}
}
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>
<html>
<head>
<title>laravel 8 upload image example - scratchcoding.dev</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<div class="panel panel-primary">
<div class="panel-heading"><h2>laravel 8 upload image example - scratchcoding.dev</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>
</div>
<img src="images/{{ Session::get('image') }}">
@endif
@if (count($errors) > 0)
<div class="alert alert-danger">
<strong>Oops!</strong> There were some problems with your input.
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('upload.image.post') }}" method="POST" enctype="multipart/form-data">
@csrf
<div class="row">
<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>
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.