How to upload images in Laravel 10

How to upload images in Laravel 10

In this article, We will learn about Laravel 10. How to upload images using the Laravel 10 by following the step-by-step process. If you don’t know about Laravel, I will tell you some basic details about Laravel 10. Laravel is a Framework of the PHP Language. Laravel is provided to develop web applications. It’s easy to learn and develop web applications with the Model, View, and controller. If you learn more about Laravel please follow my Youtube channel and learn all about Laravel. 

Let’s see How to upload an image using Laravel. After installing Laravel open your project and follow the steps below.

Step-1:-  I am going to create a controller that handles the request and response to the user. To create the Controller you just follow the command below. 

php artisan make:controller UploadImageController

Now you will see, this fill created inside of the controller folder.  After that just open your controller filer and follow the codes. 

<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
class UploadImageController extends Controller
{
    // View File To Upload Image
    public function index()
{
        return view('upload-image');
    }


    public function storeImage(Request $request)
    {
     // Validate Image
        $request->validate([
            'image' => 'required|image|mimes:png,jpg,jpeg'
        ]);
     // Change the file name
        $imageName = time().'.'.$request->image->extension();
        // Public Folder
        $request->image->move(public_path('images'), $imageName);
        // //Store in Storage Folder
        // $request->image->storeAs('images', $imageName);
        return back()->with('success', 'Image uploaded Successfully!')
        ->with('image', $imageName);
    }
}

Step - 2:  After that, click on the routes folder under the web.php file and define the route path for accessing the controller URL via a web browser.

use App\Http\Controllers\admin\UploadImageController;
Route::get('/upload-image',[UploadImageController::class,'index'])->name('image.form');
Route::post('/upload-image-post',[UploadImageController::class,'storeImage'])->name('image.store');

Step - 3:  Now it's time to create a view file, open the resources/views folder create a file named upload-image.blade.php, and update the file with the code below.

@extends('app')
@section('content')
    <div id="contact" class="container">
        <h1 class="text-center" style="margin-top: 100px">Image Upload</h1>
        @if ($message = Session::get('success'))
            <div class="alert alert-success alert-block">
                <strong>{{$message}}</strong>
            </div>
            <img src="{{ asset('images/'.Session::get('image')) }}" />
        @endif
        <form method="POST" action="{{ route('image.store') }}" enctype="multipart/form-data">
            @csrf
            <input type="file" class="form-control" name="image" />
            <button type="submit" class="btn btn-sm">Upload</button>
        </form>
    </div>
@endsection

After saving all the files. Go to the web browser and type the access URL like  http://127.0.0.1:8000/upload-image then you will see just the below image.

How to upload images in Laravel 10

and once you select an image and upload it you will see the success message with the image.


How to upload images in Laravel 10

I hope you will learn about uploading images via Laravel. 

Recommended Posts:

  1. How To Create and Validate Form in Laravel 9
  2. Laravel 9 Send Mail using queue in Laravel
  3. w to enable the query log in laravel
  4. Laravel change password with current password validation example
  5. How to Install Laravel 9 in Windows 10 and windows 11
  6. How to create Helpers function in Laravel
  7. How to upload images in Laravel 10
  8. Mastering the Art of Middleware: A Step-by-Step Guide to Creating Seamless Connections.
  9. Laravel Custom Login and Registration Example
  10. How to Upload File in Laravel 9 with Validation
  11. Laravel 9 CRUD Example Tutorial | Laravel 9 CRUD for Beginners
  12. How to Install Laravel 9 in Windows 10 and windows 11