Upload Gambar Laravel

1.Di tabel pasiens, kamu harus punya kolom foto

$table->string('foto')->nullable();

Lakukan migrate dengan menambahkan foto

php artisan make:migration add_foto_to_pasien_table --table=pasien

Di migration yang baru itu, tambahkan kode:

public function up(): void
{
    Schema::table('pasien', function (Blueprint $table) {
        $table->string('foto')->nullable()->after('kelurahan_id');
    });
}

public function down(): void
{
    Schema::table('pasien', function (Blueprint $table) {
        $table->dropColumn('foto');
    });
}

tambahkan encytype dan input untuk foto pada form

<form action="{{ route('pasien.store') }}" method="POST" enctype="multipart/form-data">
    @csrf
    <input type="text" name="kode" placeholder="Kode"><br>
    <input type="text" name="name" placeholder="Nama"><br>
    <input type="text" name="tmp_lahir" placeholder="Tempat Lahir"><br>
    <input type="date" name="tgl_lahir"><br>

    <select name="gender">
        <option value="L">Laki-laki</option>
        <option value="P">Perempuan</option>
    </select><br>

    <input type="email" name="email" placeholder="Email"><br>
    <textarea name="alamat" placeholder="Alamat"></textarea><br>

    <select name="kelurahan_id">
        @foreach ($kelurahan as $k)
            <option value="{{ $k->id }}">{{ $k->name }}</option>
        @endforeach
    </select><br>

    <input type="file" name="foto"><br> <!-- Tambahan input gambar -->

    <button type="submit">Simpan</button>
</form>

Update data pada controller

<?php

namespace App\Http\Controllers;

use App\Models\Pasien;
use App\Models\Kelurahan;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Storage;

class PasienController extends Controller
{
    public function index()
    {
        $data_pasien = Pasien::all();
        return view("pasien.pasien", compact("data_pasien"));
    }

    public function create()
    {
        $kelurahan = Kelurahan::all(); 
        return view("pasien.tambah_pasien", compact("kelurahan"));
    }

    public function store(Request $request)
    {
        $request->validate([
            'kode' => 'required|max:10',
            'name' => 'required',
            'tmp_lahir' => 'required',
            'tgl_lahir' => 'required|date',
            'gender' => 'required',
            'email' => 'required|email',
            'alamat' => 'required',
            'kelurahan_id' => 'required|exists:kelurahan,id',
            'foto' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
        ]);

        $data = $request->all();

        if ($request->hasFile('foto')) {
            $path = $request->file('foto')->store('pasien_foto', 'public');
            $data['foto'] = $path;
        }

        Pasien::create($data);

        return redirect()->route('pasien.index')->with('success', 'Data pasien berhasil ditambahkan');
    }

    public function edit($id)
    {
        $pasien = Pasien::findOrFail($id);
        $kelurahan = Kelurahan::all();
        return view('pasien.edit_pasien', compact('pasien', 'kelurahan'));
    }

    public function update(Request $request, $id)
    {
        $request->validate([
            'kode' => 'required|max:10',
            'name' => 'required',
            'tmp_lahir' => 'required',
            'tgl_lahir' => 'required|date',
            'gender' => 'required',
            'email' => 'required|email',
            'alamat' => 'required',
            'kelurahan_id' => 'required|exists:kelurahan,id',
            'foto' => 'nullable|image|mimes:jpg,jpeg,png|max:2048',
        ]);

        $pasien = Pasien::findOrFail($id);
        $data = $request->all();

        // kalau ada upload foto baru
        if ($request->hasFile('foto')) {
            // hapus foto lama kalau ada
            if ($pasien->foto && Storage::disk('public')->exists($pasien->foto)) {
                Storage::disk('public')->delete($pasien->foto);
            }

            // simpan foto baru
            $path = $request->file('foto')->store('pasien_foto', 'public');
            $data['foto'] = $path;
        }

        $pasien->update($data);

        return redirect()->route('pasien.index')->with('success', 'Data pasien berhasil diupdate');
    }

    public function destroy($id)
    {
        $pasien = Pasien::findOrFail($id);

        // hapus foto dari storage kalau ada
        if ($pasien->foto && Storage::disk('public')->exists($pasien->foto)) {
            Storage::disk('public')->delete($pasien->foto);
        }

        $pasien->delete();

        return redirect()->route('pasien.index')->with('success', 'Data pasien berhasil dihapus');
    }
}

update model

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Pasien extends Model
{
     protected $table = "pasien";

     protected $fillable = ['kode', 'name', 'tmp_lahir', 'tgl_lahir', 'gender', 'email', 'alamat', 'kelurahan_id', 'foto'];
}

update tampil data pasien

<!DOCTYPE html>
<html lang="id">
<head>
  <meta charset="UTF-8">
  <title>Puskesmas</title>
</head>
<body>
    <h2>Data Pasien</h2>
    <a href="{{ route('pasien.create') }}">+ Tambah Pasien</a>
    <table border="1">
        <thead>
            <tr>
                <th>No</th>
                <th>Kode</th>
                <th>Nama</th>
                <th>Tempat Lahir</th>
                <th>Tgl Lahir</th>
                <th>Gender</th>
                <th>email</th>
                <th>Alamat</th>
                <th>Kelurahan</th>
                <th>foto</th>
                <th>Action</th>
            </tr>

            @foreach ($data_pasien as $pasien )    
            <tr>
            <td>{{ $pasien->id}}</td>
            <td>{{ $pasien->kode}}</td>
            <td>{{ $pasien->name}}</td>
            <td>{{ $pasien->tmp_lahir}}</td>
            <td>{{ $pasien->tgl_lahir}}</td>
            <td>{{ $pasien->gender === 'L' ? 'Laki-laki' : 'Perempuan'}}</td>
            <td>{{ $pasien->email}}</td>
            <td>{{ $pasien->alamat}}</td>
            <td>{{ $pasien->kelurahan_id}}</td>
            <td>
    @if ($pasien->foto)
        <img src="{{ asset('storage/' . $pasien->foto) }}" alt="Foto Pasien" width="100">
    @else
        Tidak ada foto
    @endif
</td>


            <td><a href="{{ route('pasien.edit', $pasien->id) }}">Edit</a> | 
            <form action="{{ route('pasien.destroy', $pasien->id) }}" method="POST" style="display: inline;" onsubmit="return confirm('Yakin ingin hapus?')">
        @csrf
        @method('DELETE')
        <button type="submit" style="background: none; border: none; color: red; cursor: pointer;">๐Ÿ—‘๏ธ Hapus</button>
        </form></td>
            </tr>
             @endforeach
        </thead>
    </table>
</body>

update edit pasien

<form action="{{ route('pasien.update', $pasien->id) }}" method="POST" enctype="multipart/form-data">
    @csrf
    @method('PUT')

    <input type="text" name="kode" value="{{ $pasien->kode }}"><br>
    <input type="text" name="name" value="{{ $pasien->name }}"><br>
    <input type="text" name="tmp_lahir" value="{{ $pasien->tmp_lahir }}"><br>
    <input type="date" name="tgl_lahir" value="{{ $pasien->tgl_lahir }}"><br>

    <select name="gender">
        <option value="L" {{ $pasien->gender == 'L' ? 'selected' : '' }}>Laki-laki</option>
        <option value="P" {{ $pasien->gender == 'P' ? 'selected' : '' }}>Perempuan</option>
    </select><br>

    <input type="email" name="email" value="{{ $pasien->email }}"><br>
    <textarea name="alamat">{{ $pasien->alamat }}</textarea><br>

    <select name="kelurahan_id">
        @foreach ($kelurahan as $k)
            <option value="{{ $k->id }}" {{ $pasien->kelurahan_id == $k->id ? 'selected' : '' }}>
                {{ $k->name }}
            </option>
        @endforeach
    </select><br>

    {{-- Tampilkan foto lama jika ada --}}
    @if ($pasien->foto)
        <img src="{{ asset('storage/' . $pasien->foto) }}" alt="Foto Pasien" width="100"><br>
    @endif

    {{-- Input upload foto baru --}}
    <input type="file" name="foto"><br>

    <button type="submit">Update</button>
</form>
0
Subscribe to my newsletter

Read articles from Astrid Annasya Putri directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Astrid Annasya Putri
Astrid Annasya Putri