form transaksi dalam laravel

Auliya
10 Views
Untuk membuat form transaksi dalam Laravel, berikut adalah langkah-langkah yang dapat Anda ikuti: Buat Migrasi dan Model Transaksi: Gunakan perintah artisan untuk membuat migrasi transaksi: shell Copy code php artisan make:migration create_transactions_table --create=transactions Buka file migrasi yang baru dibuat (misalnya database/migrations/2023_05_24_000000_create_transactions_table.php) dan tentukan skema kolom untuk tabel transaksi. Contoh skema sederhana: php Copy code <?php use Illuminate\Database\Migrations\Migration; use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Schema; class CreateTransactionsTable extends Migration { public function up() { Schema::create('transactions', function (Blueprint $table) { $table->id(); $table->string('name'); $table->integer('amount'); $table->timestamps(); }); } public function down() { Schema::dropIfExists('transactions'); } } Jalankan migrasi untuk membuat tabel transaksi: shell Copy code php artisan migrate Buat model Eloquent untuk transaksi dengan perintah artisan: shell Copy code php artisan make:model Transaction Rute Transaksi: Buka file routes/web.php dan tambahkan rute untuk modul transaksi: php Copy code Route::get('/transactions', 'TransactionController@index'); Route::get('/transactions/create', 'TransactionController@create'); Route::post('/transactions', 'TransactionController@store'); Controller Transaksi: Buat controller TransactionController dengan perintah artisan: shell Copy code php artisan make:controller TransactionController Di dalam TransactionController, tambahkan metode berikut: php Copy code <?php namespace App\Http\Controllers; use App\Models\Transaction; use Illuminate\Http\Request; class TransactionController extends Controller { public function index() { $transactions = Transaction::all(); return view('transactions.index', compact('transactions')); } public function create() { return view('transactions.create'); } public function store(Request $request) { $data = $request->validate([ 'name' => 'required', 'amount' => 'required|numeric', ]); Transaction::create($data); return redirect('/transactions')->with('success', 'Transaksi berhasil ditambahkan.'); } } View Transaksi: Buat view untuk menampilkan daftar transaksi. Misalnya, buat file resources/views/transactions/index.blade.php dan tambahkan kode berikut: html Copy code <!DOCTYPE html> <html> <head> <title>Daftar Transaksi</title> </head> <body> <h1>Daftar Transaksi</h1> <a href="/transactions/create">Tambah Transaksi Baru</a> <table> <thead> <tr> <th>ID</th> <th>Nama</th> <th>Jumlah</th> <th>Tanggal Dibuat</th> </tr> </thead> <tbody> @foreach($transactions as $transaction) <tr> <td>{{ $transaction->id }}</td> <td>{{ $transaction->name }}</td>

Latest Videos

Partner Program Latest Videos Terms of Service About Us Copyright Cookie Privacy Contact