Laravel Artisan Create Model: A Beginner's Guide

Learn how to create models in Laravel with the artisan command. Our guide for beginners covers everything you need to know.

Apr 20 2025

Learn how to create models in Laravel with the artisan command. Our guide for beginners covers everything you need to know.

Langkah 1 - Konfigurasi Koneksi Database

Hal pertama yang akan kita lakukan adalah mengatur koneksi dari aplikasi (Laravel) ke dalam database. Di Laravel kita sudah disesiakan sebuah file konfigurasi untuk mempermudah developer atau pengembang, nama file tersebut bernama .env .

Jadi, silahkan teman-teman buka project Laravel-nya menggunakan Text Editor, kemudian buka file .env dan cari kode berikut ini.

DB_DATABASE=laravel DB_USERNAME=root DB_PASSWORD= 

Kemudian silahkan ubah, menjadi seperti berikut ini.

DB_DATABASE=db_laravel_10 DB_USERNAME=root DB_PASSWORD= 

Pada perubahan kode di atas, kita mengubah DB_DATABASE menggunakan db_laravel_10 dan untuk DB_PASSWORD silahkan disesuaikan dengan konfigurasi dari MySQL-nya masing-masing, jika menggunakan XAMPP, maka dikosongkan saja (default).

Laravel artisan create model

  • Langkah 1 - Konfigurasi Koneksi Database
  • Langkah 2 - Membuat Database di MySQL
  • Langkah 3 - Membuat Model dan Migration
  • Langkah 4 - Menambahkan Field di dalam Migration
  • Langkah 5 - Konfigurasi Mass Assignment
  • Langkah 6 - Menjalankan Migration

Halo teman-teman semuanya, pada artikel kali ini kita akan lanjutkan belajar bagaimana cara membuat Model dan Migration di dalam Laravel.

Model merupakan salah satu arsitektur / pola yang digunakan untuk membangun sebuah aplikasi, yaitu MVC atau Model, View dan Controller. Model biasanya berisi kode-kode yang berhubungan dengan database.

Sedangkan Migration merupakan version control dari database, dengan Migration kita bisa membuat schema table dan field dengan lebih mudah dibandingkan harus membuatnya secara manual di database.

Example

I've based this example of the model included in your question.

As I said; it's far from complete, the following additions/improvements come to mind:

  1. Determine relations and base foreign keys on that. (Take a look at this post to get some inspiration.)
  2. Add more data types to the switch .
  3. .

Save the following trait as app/SchemaBuilder.php :

Create the following controller and register a route so that you can access it:

', $buildSchemaFor)); if ((new \MigrationBuilder)->makeMigrationFile()) < echo 'Migration file successfully created.'; >else < echo 'Encountered error while making migration file.'; ># Or alternatively, print the migration file to the browser: // (new \MigrationBuilder)->printMigration(); > > 

Conclusion

Creating models in Laravel is a fundamental step in building your application’s data layer. Models provide a clean and efficient way to interact with your database, handle data validation, and define custom logic.

For those who want to take their Laravel development to the next level, consider data scopes, value objects, etc. These advanced techniques can further enhance your application’s organization and maintainability.

So, need help with creating or customizing models in your Laravel project? Let our Laravel professionals help you with it.

Chhaya Mehrotra

Tegs: