Laravel 9 - Upload FIle

Hallo, pada postingan kali ini saya akan memberikan contoh bagaimana membuat upload file di Laravel 9 , disini saya akan menjelaskan cara sederhana dan Langkah demi Langkah untuk membuat code upload file di Laravel 9 tentunya.

Dalam tutorial ini, kita akan membuat dua Routes. Logika sederhana nya kita membuat sebuah form untuk upload file kemudian file yang kit masukan akan di upload kedalam direktori yang kita tentukan.

Silahkan Ikuti Langkah Langkah Berikut :

1. Install Laravel

composer create-project laravel/laravel example-app

 

2. Buat Controller

app/Http/Controllers/ImageController.php

<?php

namespace AppHttpControllers;
use IlluminateHttpRequest;

class ImageController extends Controller

{


    public function index()

    {

        return view('imageUpload');

    }

    public function store(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);

    

        return back()

            ->with('success','Berhasil Upload Gambar.')

            ->with('image',$imageName); 

    }

}

 

3. Membuat Routes

 

<?php

use IlluminateSupportFacadesRoute;
use AppHttpControllersImageController;

  
Route::controller(ImageController::class)->group(function(){

    Route::get('image-upload', 'index');

    Route::post('image-upload', 'store')->name('image.store');

});

 

 

4.  Membuat View Pada File Blade

resources/views/imageUpload.blade.php

<!DOCTYPE html>
<head>

    <title>Laravel 9 Upload Gambar</title>

    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">

</head>

<body>

<div class="container">
    <div class="panel panel-primary">
      <div class="panel-heading">
        <h2>Laravel 9 Upload Gambar</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

      

        <form action="{{ route('image.store') }}" method="POST" enctype="multipart/form-data">

            @csrf

  

            <div class="mb-3">

                <label class="form-label" for="inputImage">Image:</label>

                <input 

                    type="file" 

                    name="image" 

                    id="inputImage"

                    class="form-control @error('image') is-invalid @enderror">
                @error('image')

                    <span class="text-danger">{{ $message }}</span>

                @enderror

            </div>

            <div class="mb-3">

                <button type="submit" class="btn btn-success">Upload</button>

            </div>

        </form>

      </div>

    </div>

</div>

</body>
</html>

 

Langkah Terkhir tinggal perintah

php artisan serve

 

SIlahkan Run code u=yang sudah kita buat :

http://localhost:8000/image-upload

 

 

 

image

Mazadie

Assalamualikum .. Saya adalah Seorang Web Developer Asal Riau.

Komentar