CRUD Laravel 10 CREATE,READ,UPDATE AND DELETE
Pada artikel ini, kita akan melihat operasi laravel 10 crud langkah demi langkah. Di sini, kita akan belajar tentang cara membuat operasi kasar di laravel 10 langkah demi langkah. Seperti yang kita tahu Laravel 10 resmi dirilis dengan banyak fungsi baru. Di laravel 10 menyediakan paket Laravel Pennant baru, aturan validasi yang dapat dipanggil, deklarasi tipe asli, dan banyak lagi.
Dalam versi laravel 10 membutuhkan versi PHP minimal 8.1.
Jadi, mari kita lihat cara membuat operasi crud di laravel 10, contoh operasi crud laravel 10, operasi crud laravel 10 langkah demi langkah, operasi crud di laravel 10, operasi crud di laravel 10 langkah demi langkah.
Dalam operasi crud laravel 10 ini, kita akan melihat membuat, membaca, memperbarui, dan menghapus dengan aturan validasi. Selain itu, kami akan membuat migrasi untuk operasi crud laravel 10.
Contoh operasi CRUD laravel 10
Langkah 1: Instal Laravel 10 Untuk Operasi CRUD
Langkah 2: Siapkan Konfigurasi Database
Langkah 3: Buat Migrasi
Langkah 4: Tambahkan Rute Sumber Daya
Langkah 5: Tambahkan Pengontrol dan Model
Langkah 6: Tambahkan File Blade
Langkah 7: Jalankan Operasi CRUD Laravel 10
DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=pos
DB_USERNAME=root
DB_PASSWORD=
php artisan make:migration create_Kategori_table --create=Kategori
Migration:<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
class BuatKategoriTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('kategori', function (Blueprint $table) {
$table->increments('id_kategori');
$table->string('nama_kategori')->unique();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('kategori');
}
}
Sekarang, jalankan migrasi seperti di bawah ini.php artisan migrate
<?php
use Illuminate\Support\Facades\Route;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\{
KategoriController,
};
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider and all of them will
| be assigned to the "web" middleware group. Make something great!
|
*/
Route::get('/', function () {
return view('welcome');
});
Route::middleware(['auth'])->group(function () {
Route::get('kategori', [KategoriController::class, 'index'])->name('kategori.index');
Route::get('kategori/create', [KategoriController::class, 'create'])->name('kategori.create');
Route::post('kategori/store', [KategoriController::class, 'store'])->name('kategori.store');
Route::get('kategori/edit/{id}', [KategoriController::class, 'edit'])->name('kategori.edit');
Route::patch('kategori/update/{id}', [KategoriController::class, 'update'])->name('kategori.update');
Route::delete('kategori/delete/{id}', [KategoriController::class, 'destroy'])->name('kategori.destroy');
});
});
php artisan make:controller KategoriController --resource --model=Kategori
Sekarang, kami akan melakukan perubahan pada Model Produk. tambahkan nama kolom ke model.<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
class Kategori extends Model
{
use HasFactory;
protected $table = 'kategori';
protected $primaryKey = 'id_kategori';
protected $guarded = [];
}
Setelah mengubah model, kita perlu menambahkan kode pada file KategoriController.php .<?php
namespace App\Http\Controllers;
use Illuminate\Http\Request;
use App\Models\Kategori; //add Student Model - Data is coming from the database via Model.
class KategoriController extends Controller
{
/**
* Display a listing of the resource.
*
* @return \Illuminate\Http\Response
*/
public function index()
{
$kategori = Kategori::all();
return view ('kategori.index')->with('kategori', $kategori);
}
/**
* Show the form for creating a new resource.
*
* @return \Illuminate\Http\Response
*/
public function create()
{
return view('kategori.create');
}
/**
* Store a newly created resource in storage.
*
* @param \Illuminate\Http\Request $request
* @return \Illuminate\Http\Response
*/
public function store(Request $request)
{
$input = $request->all();
Kategori::create($input);
return redirect('kategori')->with('flash_message', 'Student Addedd!');
}
/**
* Display the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function show($id_kategori)
{
$kategori = Kategori::find($id_kategori);
return view('kategori.show')->with('kategori', $kategori);
}
/**
* Show the form for editing the specified resource.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function edit($id_kategori)
{
$kategori = Kategori::find($id_kategori);
return view('kategori.edit')->with('kategori', $kategori);
}
/**
* Update the specified resource in storage.
*
* @param \Illuminate\Http\Request $request
* @param int $id
* @return \Illuminate\Http\Response
*/
public function update(Request $request, $id_kategori)
{
$kategori = Kategori::find($id_kategori);
$input = $request->all();
$kategori->update($input);
return redirect('kategori')->with('flash_message', 'student Updated!');
}
/**
* Remove the specified resource from storage.
*
* @param int $id
* @return \Illuminate\Http\Response
*/
public function destroy($id_kategori)
{
Kategori::destroy($id_kategori);
return redirect('kategori')->with('flash_message', 'Student deleted!');
}
}
1. layout.blade.php
2. index.blade.php
3. create.blade.php
4. edit.blade.php
<!DOCTYPE html>
<html>
<head>
<title>How To Create CRUD Operation In Laravel 10 - Techsolutionstuff</title>
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.0.0-alpha/css/bootstrap.css" rel="stylesheet">
</head>
<body>
<body class="font-sans antialiased">
<x-banner />
<div class="layout-wrapper layout-content-navbar">
<div class="layout-container bg-gray-100 ">
@livewire('navigation-menu')
<!-- Page Content -->
<main>
{{ $slot }}
</main>
</div>
</div>
@stack('modals')
@livewireScripts
</body>
</body>
</html>
<x-app-layout>
<div class="container-xxl flex-grow-1 container-p-y ">
<div class="container">
<h2>Data Kategori</h2>
<a href="{{ route('kategori.create') }}" class="btn btn-primary mb-3">Tambah Kategori</a>
@if(session('success'))
<div class="alert alert-success">
{{ session('success') }}
</div>
@endif
<table class="table">
<thead>
<tr>
<th>ID</th>
<th>Nama</th>
<th>Aksi</th>
</tr>
</thead>
<tbody>
@foreach($kategori as $item)
<tr>
<td>{{ $item->id_kategori }}</td>
<td>{{ $item->nama_kategori}}</td>
<td>
<form action="{{ route('kategori.destroy', $item->id_kategori) }}" method="POST">
<a href="{{ route('kategori.edit', $item->id_kategori) }}" class="btn btn-warning btn-sm">Edit</a>
@csrf
@method('DELETE')
<input type="hidden" name="kategori" value="{{ $item->id_kategori }}">
<button type="submit" class="btn btn-danger btn-sm" onclick="return confirm('Apakah Anda yakin ingin menghapus kategori ini?')">Hapus</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
</div>
</div>
</x-app-layout>
<x-app-layout>
<div class="container-xxl flex-grow-1 container-p-y ">
<div class="container">
<a href="{{ URL::previous() }}"> <button type="button" class="btn btn-danger mb-3"><i class='bx bx-arrow-back'></i></button></a>
<h2>Tambah Kategori</h2>
@if ($errors->any())
<div class="alert alert-danger">
<ul>
@foreach ($errors->all() as $error)
<li>{{ $error }}</li>
@endforeach
</ul>
</div>
@endif
<form action="{{ route('kategori.store') }}" method="POST">
@csrf
<div class="form-group">
<label for="nama_kategori">Nama:</label>
<input type="text" class="form-control" id="nama_kategori" name="nama_kategori" required>
</div>
<button type="submit" class="btn btn-primary mt-3">Simpan</button>
</form>
</div>
</div>
</x-app-layout>
<div class="container-xxl flex-grow-1 container-p-y ">
<a href="{{ URL::previous() }}"> <button type="button" class="btn btn-danger mb-3 mt-5">Back</button></a>
<h4>Edit kategori</h4>
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="card-body">
<form action="{{ route('kategori.update', $kategori->id_kategori) }}" method="POST">
@csrf
@method('PATCH')
<div class="mb-3">
<label for="nama" class="form-label">nama kategori</label>
<input type="text" name="nama_kategori" id="nama_kategori" class="form-control"
value="{{ old('nama_kategori') ?? $kategori->nama_kategori }}" required>
</div>
<button type="update" class="btn btn-primary">Update</button>
</form>
<script src="https://cdn.jsdelivr.net/npm/sweetalert2@11"></script>
<script>
@if (session('success'))
Swal.fire({
icon: 'success',
title: 'Success',
text: '{{ session('success') }}',
showConfirmButton: false,
timer: 1000
}).then(function() {
window.location.href = '{{ route('kategori.index') }}';
});
@endif
</script>
</div>
</div>
</div>
</div>
</div>
php artisan serve
mantap bang 👍