Laravel seeding memudahkan kita dalam mengisi banyak data sekaligus ke dalam database. Dengan sekali perintah, kita dapat memasukan data atau record ke tabel database yang mungkin kita gunakan sebagai data contoh atau testing untuk aplikasi yang sedang kita bangun.
Penggunaan
Cara membuat dan penggunaanya adalah sebagai berikut. Kita intall laravel terlebih dahulu.
laravel new blog
Silahkan buat database MySQL baru lalu hubungkan dengan aplikasi.
//.env DB_CONNECTION=mysql DB_HOST=127.0.0.1 DB_PORT=3306 DB_DATABASE=db_blog DB_USERNAME=root DB_PASSWORD=
Selanjutnya, jika di lihat pada folder migrations, terdapat file migrasi untuk tabel users. Untuk contoh yang pertama, mari kita buat seeder untuk mengisi record tabel users tersebut.
Untuk membuat file seeder, kita gunakan perintah artisan make:seeder [nama-file-seeder]
seperti dibawah ini.
php artisan make:seeder UsersTableSeeder
Kemudian buka file seeder UsersTableSeeder.php di direktori database > seeders dan buat seperti dibawah ini.
<?php namespace Database\Seeders; use App\Models\User; use Illuminate\Database\Seeder; class UsersTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { $user = new User; $user->name = "User"; $user->email = "[email protected]"; $user->password = bcrypt('12345678'); $user->save(); } }
Kemudian buka file DatabaseSeeder.php lalu tambahkan class UsersTableSeeder di dalam method run() dengan fungsi panggilan seperti di bawah ini.
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { $this->call([ UsersTableSeeder::class ]); } }
Selanjutnya tinggal jalankan perintah migrasi untuk membuat tabel database sekaligus melakukan seeding.
~ php artisan migrate --seed
@2W7N9W3 ➜ blog php artisan migrate --seed Migration table created successfully. Migrating: 2014_10_12_000000_create_users_table Migrated: 2014_10_12_000000_create_users_table (608.63ms) Migrating: 2014_10_12_100000_create_password_resets_table Migrated: 2014_10_12_100000_create_password_resets_table (451.91ms) Migrating: 2019_08_19_000000_create_failed_jobs_table Migrated: 2019_08_19_000000_create_failed_jobs_table (834.51ms) Seeding: Database\Seeders\UsersTableSeeder Seeded: Database\Seeders\UsersTableSeeder (186.34ms) Database seeding completed successfully.
Migrasi dan seeding berhasil. Mari kita lihat pada database.
~ MariaDB [db_seed]> use contohseeding; Database changed ~ MariaDB [contohseeding]> show tables; +-------------------------+ | Tables_in_contohseeding | +-------------------------+ | failed_jobs | | migrations | | password_resets | | users | +-------------------------+ 4 rows in set (0.001 sec) ~ MariaDB [contohseeding]> desc users; +-------------------+---------------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------------+---------------------+------+-----+---------+----------------+ | id | bigint(20) unsigned | NO | PRI | NULL | auto_increment | | name | varchar(255) | NO | | NULL | | | email | varchar(255) | NO | UNI | NULL | | | email_verified_at | timestamp | YES | | NULL | | | password | varchar(255) | NO | | NULL | | | remember_token | varchar(100) | YES | | NULL | | | created_at | timestamp | YES | | NULL | | | updated_at | timestamp | YES | | NULL | | +-------------------+---------------------+------+-----+---------+----------------+ 8 rows in set (0.047 sec) ~ MariaDB [contohseeding]> select * from users; +----+------+---------------+-------------------+----------------------------------------+--------------------------------+ | id | name | email | email_verified_at | password | remember_token | created_at | updated_at | +----+------+---------------+-------------------+------------------------+----------------+---------------------+---------+ | 1 | User | [email protected] | NULL | $2y$10$24fwDgreeLmmk...| NULL | 2020-11-14...| 2020-11-14... | +----+------+---------------+-------------------+-----------------------------------------+---------------+---------------+ 1 row in set (0.000 sec)
Di atas, kita telah berhasil melakukan seeding data ke tabel users, untuk contoh lainnya, mari kita buat tabel baru dan seeding lebih dari satu data object.
Kita buat sebuah file migrasi baru untuk membuat tabel database.
php artisan make:migration create_posts_table
Silahkan buka file migrasi untuk posts dan buat seperti dibawah ini.
public function up() { Schema::create('posts', function (Blueprint $table) { $table->id(); $table->string('title'); $table->string('body'); $table->timestamps(); }); }
Kemudian kita buat file seeder untuk posts.
~ php artisan make:seeder PostsTableSeeder
Buka PostsTableSeeder.php, buat seperti dibawah ini.
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; class PostsTableSeeder extends Seeder { /** * Run the database seeds. * * @return void */ public function run() { $posts = [ [ 'title' => "Lorem Ipsum", 'body' => 'Lorem ipsum is placeholder text commonly used in the graphic, print, and publishing industries for previewing layouts and visual mockups.', 'created_at' => new \DateTime, 'updated_at' => null, ], [ 'title' => "What is Lorem Ipsum?", 'body' => 'From its medieval origins to the digital era, learn everything there is to know about the ubiquitous lorem ipsum passage.', 'created_at' => new \DateTime, 'updated_at' => null, ], [ 'title' => "Origins and Discovery", 'body' => 'Lorem ipsum began as scrambled, nonsensical Latin derived from Cicero"s 1st-century BC text De Finibus Bonorum et Malorum.', 'created_at' => new \DateTime, 'updated_at' => null, ], [ 'title' => "Fuzzy Beginnings", 'body' => 'Creation timelines for the standard lorem ipsum passage vary, with some citing the 15th century and others the 20th.', 'created_at' => new \DateTime, 'updated_at' => null, ], ]; \DB::table('posts')->insert($posts); } }
Lalu tambahkan PostsTableSeeder pada DatabaseSeeder.php.
<?php namespace Database\Seeders; use Illuminate\Database\Seeder; class DatabaseSeeder extends Seeder { /** * Seed the application's database. * * @return void */ public function run() { $this->call([ UsersTableSeeder::class, PostsTableSeeder::class, ]); } }
Karena UsersTableSeeder sebelumnya sudah dikirim. Kita gunakan perintah seperti dibawah ini untuk seeding sesuai class.
php artisan db:seed --class=PostsTableSeeder
MariaDB [contohseeding]> select * from posts; +----+-----------------------+-----------------------------------------------------------------+---------------------+------------+ | id | title | body | created_at | updated_at | +----+-----------------------+----------------------------------------------------------------------------------------------------+ | 1 | Lorem Ipsum | Lorem ipsum is placeholder text commonly used in the graphic... | 2020-11-14 09:56:56 | NULL | | 2 | What is Lorem Ipsum? | From its medieval origins to the digital era, learn... | 2020-11-14 09:56:56 | NULL | | 3 | Origins and Discovery | Lorem ipsum began as scrambled, nonsensical Latin derived... | 2020-11-14 09:56:56 | NULL | | 4 | Fuzzy Beginnings | Creation timelines for the standard lorem ipsum passage vary... | 2020-11-14 09:56:56 | NULL | +----+-----------------------+-----------------------------------------------------------------+---------------------+------------+ 4 rows in set (0.001 sec)
Selesai. Demikian cara seeding data di laravel 8. Silahkan dicoba.
Postingan terkait seeder: Laravel Model Factories: Fungsi dan Cara Penggunaan di Laravel 8