Share this:

Welcome to this Laravel 10 Image Upload Example Tutorial! If you’re working on a web application and need to implement image uploading, Laravel 10 provides a simple and efficient way to handle this task. In this tutorial, we’ll walk through the process of uploading and displaying images using Laravel 10’s robust features.

Prerequisites

To follow this tutorial, you should have a basic understanding of Laravel and PHP. You’ll also need the following:

  • Laravel 10 installed on your system
  • A Laravel project set up
  • A working database connection

Step 1: Create a Migration and Model

First, let’s create a table to store our image data. Run the following command to create a migration file:

php artisan make:migration create_images_table

Edit the migration file located in the database/migrations folder and add the necessary columns:

public function up()
{
    Schema::create('images', function (Blueprint $table) {
        $table->id();
        $table->string('title');
        $table->text('image_path');
        $table->timestamps();
    });
}

Now, run the migration to create the table:

php artisan migrate

Next, create an Image model to interact with the table:

php artisan make:model Image

Step 2: Create Routes and Controllers

Create a new controller for handling image uploads:

php artisan make:controller ImageController

Add the following routes to your routes/web.php file:

use App\Http\Controllers\ImageController;

Route::get('/images', [ImageController::class, 'index']);
Route::post('/images/upload', [ImageController::class, 'store']);

Step 3: Implement the ImageController

Open the ImageController.php file and add the following code:

use App\Models\Image;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

public function index()
{
    $images = Image::all();
    return view('images.index', compact('images'));
}

public function store(Request $request)
{
    $request->validate([
        'title' => 'required',
        'image' => 'required|image|max:2048',
    ]);

    $imagePath = $request->file('image')->store('public/images');
    $image = new Image([
        'title' => $request->get('title'),
        'image_path' => $imagePath,
    ]);
    $image->save();

    return redirect('/images')->with('success', 'Image uploaded successfully');
}

Step 4: Create the Blade Views

Create a new folder named images inside the resources/views directory. Inside that folder, create a new file named index.blade.php and add the following code:

@extends('layouts.app')

@section('content')
<div class="container">
    <h2>Image Upload</h2>
    <form action="{{ url('/images/upload') }}" method="POST" enctype="multipart/form-data">
        @csrf
        <div class="form-group">
            <label for="title">Title</label>
            <input type="text" name="title" id="title" class="form-control" required>
        </div>
        <div class="form-group">
            <label for="image">Image</label>
            <input type="file" name="image" id="image" class="form-control" required>
        </div>
        <button type="submit" class="btn btn-primary">Upload</button>
    </form>
    <hr>

    <h2>Uploaded Images</h2>
    <div class="row">
        @foreach($images as $image)
            <div class="col-md-4">
                <div class="card mb-4">
                    <img src="{{ Storage::url($image->image_path) }}" alt="{{ $image->title }}" class="card-img-top">
                    <div class="card-body">
                        <h5 class="card-title">{{ $image->title }}</h5>
                    </div>
                </div>
            </div>
        @endforeach
    </div>
</div>
@endsection

This view displays a form for uploading images and shows the uploaded images in a grid format.

Step 5: Update the App Layout

In the resources/views/layouts/app.blade.php file, add the following line inside the <head> section to enable the display of success messages:

@if (session('success'))
    <div class="alert alert-success">{{ session('success') }}</div>
@endif

This code will display a success message when an image is uploaded.

Step 6: Test the Image Upload Functionality

Now that everything is set up, start your Laravel development server by running:

php artisan serve

Visit http://localhost:8000/images in your web browser, and you should see the image upload form. You can now upload images and see them displayed on the page.

Conclusion

In this Laravel 10 Image Upload Example Tutorial, we’ve shown how to implement image uploading and display functionality using Laravel’s built-in features. You can easily customize this example to fit your specific requirements or integrate it into your existing Laravel application. The Laravel framework makes it simple to handle file uploads and process them efficiently, making it a great choice for web developers.

Share this:

Categorized in: