> For the complete documentation index, see [llms.txt](https://learn.devlabss.my.id/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://learn.devlabss.my.id/laravel/bab-13-pengayaan-proyek-and-portofolio/13.3-upload-file-sederhana.md).

# 13.3 Upload File Sederhana

## Upload file sederhana

Aplikasi perpustakaan akan terasa lebih hidup jika buku punya cover.

Di subbab ini, Anda menambahkan upload file gambar sederhana.

### Target praktik subbab ini

Setelah menyelesaikan subbab ini, Anda seharusnya bisa:

* menambahkan input file ke form buku
* memvalidasi file gambar
* menyimpan file ke folder penyimpanan Laravel
* menyimpan nama file ke database
* menampilkan cover buku di halaman daftar atau detail

### Langkah praktik step-by-step

{% stepper %}
{% step %}

### Tambahkan kolom cover ke tabel `buku`

Buat migration baru untuk menambah kolom misalnya `cover`.

Jalankan migration setelah file selesai dibuat.
{% endstep %}

{% step %}

### Tambahkan input file ke form create dan edit

Di form buku, tambahkan field file.

Pastikan form memakai `enctype="multipart/form-data"`.

Tanpa itu, file tidak akan ikut terkirim.
{% endstep %}

{% step %}

### Tambahkan validasi file di controller

Validasi minimal yang aman:

* file boleh kosong saat awal
* jika ada, harus berupa gambar
* ukuran file dibatasi

Contoh aturan yang bisa dipakai:

```php
'cover' => 'nullable|image|max:2048'
```

{% endstep %}

{% step %}

### Simpan file ke storage

Di method `store()` atau `update()`, simpan file yang diunggah ke folder misalnya `covers`.

Lalu simpan path atau nama file itu ke kolom `cover` pada tabel `buku`.
{% endstep %}

{% step %}

### Tampilkan gambar cover di view

Saat data buku ditampilkan, cek apakah cover tersedia.

Kalau ada, tampilkan gambarnya.

Kalau belum ada, tampilkan teks seperti `Belum ada cover`.
{% endstep %}
{% endstepper %}

### Output yang harus terlihat

Setelah subbab ini selesai, hasil berikut seharusnya sudah terlihat:

* tabel `buku` punya kolom `cover`
* form buku punya input upload file
* file gambar berhasil tersimpan ke storage
* nama atau path file tersimpan di database
* halaman buku menampilkan cover jika file tersedia

### Troubleshooting yang paling sering

Masalah yang paling sering muncul:

* **file tidak ikut terkirim** — form belum memakai `multipart/form-data`
* **gambar gagal tampil** — path file salah atau link storage belum siap
* **validasi selalu gagal** — aturan file tidak sesuai dengan file yang diuji
* **kolom `cover` tidak ada** — migration belum dijalankan

{% hint style="warning" %}
Jangan menerima semua jenis file tanpa validasi. Batasi hanya gambar dan ukuran yang wajar.
{% endhint %}

### Mini tugas mandiri

Kerjakan tugas kecil ini:

* tambahkan cover pada minimal tiga data buku
* tampilkan cover kecil di tabel daftar buku
* tampilkan fallback `Belum ada cover` jika file kosong

### Ringkasan subbab

Di subbab ini, data buku tidak lagi hanya berupa teks.

Anda menambahkan upload gambar sederhana agar aplikasi terasa lebih lengkap dan lebih realistis.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://learn.devlabss.my.id/laravel/bab-13-pengayaan-proyek-and-portofolio/13.3-upload-file-sederhana.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
