> 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-5-migration-and-seeder/5.4-membuat-data-palsu-dengan-seeder.md).

# 5.4 Membuat Data Palsu dengan Seeder

## Membuat data palsu dengan seeder

Tabel `buku` sudah berhasil dibuat.

Sekarang tabel itu masih kosong.

Agar fitur daftar buku bisa diuji, Anda perlu menambahkan data contoh.

Di Laravel, pekerjaan ini dilakukan dengan **seeder**.

Seeder membantu Anda mengisi database secara cepat tanpa input manual satu per satu.

### Apa itu seeder

Seeder adalah file yang berisi perintah untuk memasukkan data ke database.

Biasanya seeder dipakai untuk:

* menyiapkan data uji
* mengisi data awal aplikasi
* mempercepat proses testing

Untuk modul ini, Anda akan membuat beberapa data buku dummy.

### Membuat seeder untuk tabel `buku`

Ikuti langkah berikut.

{% stepper %}
{% step %}

### Buat file seeder

Buka terminal proyek Laravel.

Lalu jalankan:

```bash
php artisan make:seeder BukuSeeder
```

Jika berhasil, Laravel akan membuat file baru di folder `database/seeders`.
{% endstep %}

{% step %}

### Buka file `BukuSeeder.php`

Buka folder:

```
database/seeders
```

Lalu buka file `BukuSeeder.php`.
{% endstep %}

{% step %}

### Isi method `run()`

Tambahkan kode berikut:

```php
<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;

class BukuSeeder extends Seeder
{
    public function run(): void
    {
        DB::table('buku')->insert([
            [
                'judul' => 'Belajar Laravel 11 untuk Pemula',
                'penulis' => 'Ahmad RPL',
                'penerbit' => 'Informatika SMK',
                'tahun_terbit' => 2024,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'judul' => 'Dasar Pemrograman Objek PHP',
                'penulis' => 'Budi Setiawan',
                'penerbit' => 'Erlangga Tekno',
                'tahun_terbit' => 2025,
                'created_at' => now(),
                'updated_at' => now(),
            ],
            [
                'judul' => 'Komik Naruto: Menjadi Pembuat Web',
                'penulis' => 'Masashi Kishimoto',
                'penerbit' => 'Shonen SMK',
                'tahun_terbit' => 2026,
                'created_at' => now(),
                'updated_at' => now(),
            ],
        ]);
    }
}
```

Lalu simpan file.
{% endstep %}

{% step %}

### Jalankan seeder

Pastikan MySQL di **Laragon** sudah aktif.

Setelah itu, jalankan:

```bash
php artisan db:seed --class=BukuSeeder
```

Jika berhasil, terminal akan menampilkan status seeding selesai.
{% endstep %}
{% endstepper %}

### Memahami isi kode seeder

Bagian penting dari kode tadi adalah:

* `use Illuminate\Support\Facades\DB;` — mengimpor facade `DB`
* `DB::table('buku')` — memilih tabel tujuan
* `insert([...])` — memasukkan data baru
* `now()` — mengisi `created_at` dan `updated_at` otomatis

Karena data ditulis dalam beberapa array, Laravel akan menambahkan beberapa baris sekaligus.

Ini lebih cepat daripada input manual.

### Kapan seeder berguna

Seeder sangat berguna saat:

* halaman daftar data masih kosong
* Anda ingin menguji pencarian atau pagination
* Anda butuh data contoh yang konsisten

{% hint style="info" %}
Seeder dipakai untuk data uji atau data awal. Bukan untuk data yang diinput pengguna secara manual.
{% endhint %}

### Output yang harus terlihat

Setelah subbab ini selesai, hasil berikut seharusnya sudah terlihat:

* file `database/seeders/BukuSeeder.php` berhasil dibuat
* method `run()` berisi beberapa data dummy buku
* perintah `php artisan db:seed --class=BukuSeeder` berjalan tanpa error
* tabel `buku` berisi beberapa baris data contoh

### Troubleshooting yang paling sering

Masalah yang paling sering muncul:

* **`Class "Database\Seeders\DB" not found`** — Anda belum menulis `use Illuminate\Support\Facades\DB;`
* **`Base table or view not found`** — tabel `buku` belum dibuat, jadi jalankan `php artisan migrate`
* **seeding gagal tersambung ke database** — MySQL belum aktif atau konfigurasi `.env` masih salah

{% hint style="warning" %}
Jika tabel belum ada, selesaikan dulu langkah di bab 5.3 sebelum menjalankan seeder.
{% endhint %}

### Poin evaluasi bab 5.4

Pastikan Anda sudah memahami poin berikut:

* Saya paham fungsi seeder untuk menambahkan data dummy ke database.
* Saya tahu file seeder disimpan di `database/seeders`.
* Saya tahu perintah membuat seeder adalah `php artisan make:seeder BukuSeeder`.
* Saya bisa menulis `DB::table('buku')->insert([...])`.
* Saya paham fungsi `now()` pada data timestamp.
* Saya berhasil menjalankan `php artisan db:seed --class=BukuSeeder`.
* Saya memastikan MySQL aktif sebelum menjalankan seeder.

Jika semuanya sudah jelas, lanjut ke [5.5 Penjelasan Perintah migrate:fresh](/laravel/bab-5-migration-and-seeder/5.5-penjelasan-perintah-migrate-fresh.md).


---

# 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-5-migration-and-seeder/5.4-membuat-data-palsu-dengan-seeder.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.
