Basa Basi
Ketika membuat sebuah module di Laravel ada 3 hal utama yang harus kita buat, yaitu: Controller, Model dan Migration
- Controller: untuk menangani logika alur aplikasi seperti menerima request dari user, memproses, dan mengembalikan response (biasanya berupa view atau JSON).
- Model: Tempat meletakkan logika yang berkaitan dengan data (misal relasi antar tabel, accessor, mutator, query scope).
- Migration: Digunakan untuk membuat dan mengubah struktur database dengan catatan setiap perubahannya (seperti version control (GIT) untuk database).
Cara Biasa
Biasanya untuk membuat Controller, Model dan Migration harus menjalankan 3 command berikut:
php artisan make:migration create_table_product
php artisan make:model Product
php artisan make:controller ProductController
Shortcut
Kalau cara biasa membutuhkan 3 command tapi dengan shortcut berikut kamu cukup 1 command dan bisa buat semuanya.
php artisan make:model Product -mcr
Hasilnya :
File Migration 2025_07_23_023258_create_products_table.php
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('products', function (Blueprint $table) {
$table->id();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*/
public function down(): void
{
Schema::dropIfExists('products');
}
};
File Model Product.php
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class Product extends Model
{
//
}
File Controller ProductController.php
<?php
namespace App\Http\Controllers;
use App\Models\Product;
use Illuminate\Http\Request;
class ProductController extends Controller
{
/**
* Display a listing of the resource.
*/
public function index()
{
//
}
/**
* Show the form for creating a new resource.
*/
public function create()
{
//
}
/**
* Store a newly created resource in storage.
*/
public function store(Request $request)
{
//
}
/**
* Display the specified resource.
*/
public function show(Product $product)
{
//
}
/**
* Show the form for editing the specified resource.
*/
public function edit(Product $product)
{
//
}
/**
* Update the specified resource in storage.
*/
public function update(Request $request, Product $product)
{
//
}
/**
* Remove the specified resource from storage.
*/
public function destroy(Product $product)
{
//
}
}
Terima kasih sudah membaca dan semoga bermanfaat untuk project-project Laravel kamu !