Penggunaan Eloquent ORM(Object-Relational Mapping)
This commit is contained in:
parent
7c786bbfae
commit
177ed11260
|
|
@ -3,96 +3,66 @@
|
|||
namespace App\Http\Controllers;
|
||||
|
||||
use App\Models\Siswa;
|
||||
use App\Models\Kelas;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class SiswaController extends Controller
|
||||
{
|
||||
public function index()
|
||||
{
|
||||
$siswas = Siswa::all();
|
||||
$siswas = Siswa::with('kelas')->get();
|
||||
return view('siswa.index', compact('siswas'));
|
||||
}
|
||||
|
||||
public function create()
|
||||
{
|
||||
return view('siswa.create');
|
||||
$kelases = Kelas::all();
|
||||
return view('siswa.create', compact('kelases'));
|
||||
}
|
||||
|
||||
public function store(Request $request)
|
||||
{
|
||||
$request->validate([
|
||||
'nama' => 'required|string|max:255',
|
||||
'kelas' => 'required|string|max:50',
|
||||
'kelas_id' => 'required|exists:kelas,id',
|
||||
'umur' => 'required|integer',
|
||||
'absen' => 'required|integer',
|
||||
]);
|
||||
|
||||
Siswa::create([
|
||||
'nama' => $request->nama,
|
||||
'kelas' => $request->kelas,
|
||||
'umur' => $request->umur,
|
||||
'absen' => $request->absen,
|
||||
]);
|
||||
Siswa::create($request->all());
|
||||
|
||||
return redirect()->route('siswa.index')->with('success', 'Data berhasil ditambahkan!');
|
||||
}
|
||||
|
||||
public function edit($id)
|
||||
{
|
||||
$siswa = Siswa::find($id);
|
||||
$siswa = Siswa::findOrFail($id);
|
||||
$kelases = Kelas::all(); // <-- Ambil semua kelas dari tabel kelas
|
||||
|
||||
if (!$siswa) {
|
||||
return view('siswa.notfound', ['id' => $id]);
|
||||
}
|
||||
|
||||
return view('siswa.edit', compact('siswa', 'id'));
|
||||
return view('siswa.edit', compact('siswa', 'kelases'));
|
||||
}
|
||||
|
||||
|
||||
public function update(Request $request, $id)
|
||||
{
|
||||
$request->validate([
|
||||
'nama' => 'required|string|max:255',
|
||||
'kelas' => 'required|string|max:50',
|
||||
'kelas_id' => 'required|exists:kelas,id',
|
||||
'umur' => 'required|integer',
|
||||
'absen' => 'required|integer',
|
||||
]);
|
||||
|
||||
$siswa = Siswa::find($id);
|
||||
|
||||
if (!$siswa) {
|
||||
return view('siswa.notfound', ['id' => $id]);
|
||||
}
|
||||
|
||||
$siswa->update([
|
||||
'nama' => $request->nama,
|
||||
'kelas' => $request->kelas,
|
||||
'umur' => $request->umur,
|
||||
'absen' => $request->absen,
|
||||
]);
|
||||
$siswa = Siswa::findOrFail($id);
|
||||
$siswa->update($request->all());
|
||||
|
||||
return redirect()->route('siswa.index')->with('success', 'Data siswa berhasil diupdate!');
|
||||
}
|
||||
|
||||
public function show($id)
|
||||
{
|
||||
$siswa = Siswa::find($id);
|
||||
|
||||
if (!$siswa) {
|
||||
return view('siswa.notfound', ['id' => $id]);
|
||||
}
|
||||
|
||||
return view('siswa.detail', compact('siswa'));
|
||||
}
|
||||
|
||||
public function destroy($id)
|
||||
{
|
||||
$siswa = Siswa::find($id);
|
||||
$siswa = Siswa::findOrFail($id);
|
||||
$siswa->delete();
|
||||
|
||||
if ($siswa) {
|
||||
$siswa->delete();
|
||||
return redirect()->route('siswa.index')->with('success', 'Data berhasil dihapus.');
|
||||
}
|
||||
|
||||
return redirect()->route('siswa.index')->with('error', 'Data tidak ditemukan.');
|
||||
return redirect()->route('siswa.index')->with('success', 'Data berhasil dihapus.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Kelas extends Model
|
||||
{
|
||||
use HasFactory;
|
||||
|
||||
protected $table = 'kelas';
|
||||
protected $fillable = ['nama'];
|
||||
|
||||
public function siswas()
|
||||
{
|
||||
return $this->hasMany(Siswa::class, 'kelas_id', 'id');
|
||||
}
|
||||
}
|
||||
|
|
@ -2,13 +2,17 @@
|
|||
|
||||
namespace App\Models;
|
||||
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
|
||||
class Siswa extends Model
|
||||
{
|
||||
protected $table = 'siswa';
|
||||
use HasFactory;
|
||||
|
||||
protected $fillable = [
|
||||
'nama', 'kelas', 'umur', 'absen'
|
||||
];
|
||||
protected $fillable = ['nama', 'kelas_id', 'umur', 'absen'];
|
||||
|
||||
public function kelas()
|
||||
{
|
||||
return $this->belongsTo(Kelas::class, 'kelas_id', 'id');
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,31 +0,0 @@
|
|||
<?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('siswa', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('nama');
|
||||
$table->string('kelas');
|
||||
$table->integer('umur');
|
||||
$table->integer('absen');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::dropIfExists('siswa');
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,19 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::create('kelas', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('nama');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void {
|
||||
Schema::dropIfExists('kelas');
|
||||
}
|
||||
};
|
||||
|
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration {
|
||||
public function up(): void {
|
||||
Schema::create('siswas', function (Blueprint $table) {
|
||||
$table->id();
|
||||
$table->string('nama');
|
||||
$table->foreignId('kelas_id')->constrained('kelas')->onDelete('cascade');
|
||||
$table->integer('umur');
|
||||
$table->integer('absen');
|
||||
$table->timestamps();
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void {
|
||||
Schema::dropIfExists('siswas');
|
||||
}
|
||||
};
|
||||
|
|
@ -15,8 +15,5 @@ class DatabaseSeeder extends Seeder
|
|||
{
|
||||
// User::factory(10)->create();
|
||||
|
||||
$this->call([
|
||||
SiswaSeeder::class,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -0,0 +1,29 @@
|
|||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Kelas;
|
||||
|
||||
class KelasSeeder extends Seeder
|
||||
{
|
||||
/**
|
||||
* Run the database seeds.
|
||||
*/
|
||||
public function run(): void
|
||||
{
|
||||
$kelases = [
|
||||
'XIII SIJA A',
|
||||
'XIII SIJA B',
|
||||
'XIII RPL A',
|
||||
'XIII RPL B',
|
||||
'XII',
|
||||
'XIII'
|
||||
];
|
||||
|
||||
foreach ($kelases as $kelas) {
|
||||
Kelas::create(['nama' => $kelas]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ namespace Database\Seeders;
|
|||
|
||||
use Illuminate\Database\Console\Seeds\WithoutModelEvents;
|
||||
use Illuminate\Database\Seeder;
|
||||
use App\Models\Siswa;
|
||||
|
||||
class SiswaSeeder extends Seeder
|
||||
{
|
||||
|
|
@ -13,18 +12,21 @@ class SiswaSeeder extends Seeder
|
|||
*/
|
||||
public function run(): void
|
||||
{
|
||||
Siswa::create([
|
||||
'nama' => 'syifa',
|
||||
'kelas' => 'XIII',
|
||||
$XIII = Kelas::where('kelas', 'XIII')->first();
|
||||
$XII = Kelas::where('kelas', 'XII')->first();
|
||||
|
||||
// Cek apakah kelas tersedia
|
||||
if (!$XIII || !$XII) {
|
||||
throw new \Exception("Kelas belum tersedia. Pastikan KelasSeeder sudah jalan.");
|
||||
}
|
||||
|
||||
DB::table('siswas')->insert([
|
||||
'nama' => 'Syifa',
|
||||
'kelas_id' => 2, //ini nama kolom baru
|
||||
'umur' => 19,
|
||||
'absen' => 34,
|
||||
]);
|
||||
|
||||
Siswa::create([
|
||||
'nama' => 'syifa maulidya',
|
||||
'kelas' => 'XIII SIJA A',
|
||||
'umur' => 18,
|
||||
'absen' => 35,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -37,7 +37,7 @@
|
|||
margin-bottom: 8px;
|
||||
color: #34495e;
|
||||
}
|
||||
input {
|
||||
input, select {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
margin-bottom: 20px;
|
||||
|
|
@ -46,7 +46,7 @@
|
|||
font-size: 16px;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
input:focus {
|
||||
input:focus, select:focus {
|
||||
outline: none;
|
||||
border: 1px solid #3498db;
|
||||
box-shadow: 0 0 8px rgba(52,152,219,0.5);
|
||||
|
|
@ -89,13 +89,22 @@
|
|||
</div>
|
||||
@endif
|
||||
|
||||
@php
|
||||
use App\Models\Kelas;
|
||||
@endphp
|
||||
|
||||
<form action="{{ route('siswa.store') }}" method="POST">
|
||||
@csrf
|
||||
<label>Nama:</label>
|
||||
<input type="text" name="nama" value="{{ old('nama') }}" required>
|
||||
|
||||
<label>Kelas:</label>
|
||||
<input type="text" name="kelas" value="{{ old('kelas') }}" required>
|
||||
<select name="kelas_id" class="form-control" required>
|
||||
<option value="">-- Pilih Kelas --</option>
|
||||
@foreach($kelases as $kelas)
|
||||
<option value="{{ $kelas->id }}">{{ $kelas->nama }}</option>
|
||||
@endforeach
|
||||
</select>
|
||||
|
||||
<label>Umur:</label>
|
||||
<input type="number" name="umur" value="{{ old('umur') }}" required>
|
||||
|
|
@ -110,4 +119,4 @@
|
|||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@
|
|||
margin-bottom: 8px;
|
||||
color: #34495e;
|
||||
}
|
||||
input {
|
||||
input, select {
|
||||
width: 100%;
|
||||
padding: 12px;
|
||||
margin-bottom: 20px;
|
||||
|
|
@ -46,7 +46,7 @@
|
|||
font-size: 16px;
|
||||
transition: all 0.3s ease;
|
||||
}
|
||||
input:focus {
|
||||
input:focus, select:focus {
|
||||
outline: none;
|
||||
border: 1px solid #3498db;
|
||||
box-shadow: 0 0 8px rgba(52,152,219,0.5);
|
||||
|
|
@ -66,6 +66,14 @@
|
|||
.button:hover {
|
||||
background: #2980b9;
|
||||
}
|
||||
.alert {
|
||||
background: #f8d7da;
|
||||
color: #721c24;
|
||||
padding: 15px;
|
||||
border-radius: 10px;
|
||||
margin-bottom: 20px;
|
||||
border-left: 5px solid #f5c6cb;
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
|
|
@ -81,15 +89,26 @@
|
|||
</div>
|
||||
@endif
|
||||
|
||||
<form action="{{ route('siswa.update', $id) }}" method="POST">
|
||||
@php
|
||||
use App\Models\Kelas;
|
||||
@endphp
|
||||
|
||||
<form action="{{ route('siswa.update', $siswa->id) }}" method="POST">
|
||||
@csrf
|
||||
@method('PUT')
|
||||
|
||||
<label>Nama:</label>
|
||||
<input type="text" name="nama" value="{{ old('nama', $siswa['nama']) }}" required>
|
||||
<label>Nama:</label>
|
||||
<input type="text" name="nama" value="{{ old('nama', $siswa['nama']) }}" required>
|
||||
|
||||
<label>Kelas:</label>
|
||||
<input type="text" name="kelas" value="{{ old('kelas', $siswa['kelas']) }}" required>
|
||||
<label for="kelas_id">Kelas:</label>
|
||||
<select name="kelas_id" required>
|
||||
<option value="" disabled>-- Pilih Kelas --</option>
|
||||
@foreach ($kelases as $kelas)
|
||||
<option value="{{ $kelas->id }}" {{ $siswa->kelas_id == $kelas->id ? 'selected' : '' }}>
|
||||
{{ $kelas->nama }}
|
||||
</option>
|
||||
@endforeach
|
||||
</select>
|
||||
|
||||
<label>Umur:</label>
|
||||
<input type="number" name="umur" value="{{ old('umur', $siswa['umur']) }}" required>
|
||||
|
|
@ -104,4 +123,4 @@
|
|||
</div>
|
||||
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@
|
|||
<tr>
|
||||
<td>{{ $loop->iteration }}</td>
|
||||
<td>{{ $siswa->nama }}</td>
|
||||
<td>{{ $siswa->kelas }}</td>
|
||||
<td>{{ $siswa->kelas->nama ?? '-' }}</td>
|
||||
<td>{{ $siswa->umur }}</td>
|
||||
<td>{{ $siswa->absen }}</td>
|
||||
<td>
|
||||
|
|
|
|||
Loading…
Reference in New Issue