Skip to content

Commit

Permalink
Laravel Eloquent HasMany
Browse files Browse the repository at this point in the history
  • Loading branch information
FakhriRyu committed Oct 22, 2023
1 parent c44bb98 commit 9352d41
Show file tree
Hide file tree
Showing 16 changed files with 323 additions and 94 deletions.
26 changes: 15 additions & 11 deletions app/Http/Controllers/KelasController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use App\Models\Kelas;
use Illuminate\Http\Request;
use Illuminate\Http\Response; // Impor kelas Response

class KelasController extends Controller
{
Expand All @@ -13,13 +14,14 @@ class KelasController extends Controller
* @return \Illuminate\Http\Response
*/
public function index()
{
$kelas = Kelas::all(); // Mengambil semua data kelas dari database
{
$kelas = Kelas::all(); // Mengambil semua data kelas dari database

return view('kelas', [
'title' => 'Kelas',
'kelas' => $kelas, // Meneruskan data kelas ke tampilan
]);
// Mengembalikan tampilan sebagai respons
return view('kelas', [
'title' => 'Mata kuliah',
'kelas' => $kelas, // Meneruskan data kelas ke tampilan
]);
}


Expand Down Expand Up @@ -68,10 +70,12 @@ public function store(Request $request)
* @return \Illuminate\Http\Response
*/
public function show(Kelas $kelas)
{
$title = 'Detail Mata Kuliah - ' . $kelas->matkul; // Inisialisasi variabel $title
return view('kelas.show', compact('kelas', 'title'));
}
{
$title = 'Detail Mata Kuliah - ' . $kelas->matkul; // Inisialisasi variabel $title

// Mengembalikan tampilan sebagai respons
return view('kelas.show', compact('kelas', 'title'));
}



Expand All @@ -84,7 +88,7 @@ public function show(Kelas $kelas)
public function edit(Kelas $kelas)
{
$title = 'Edit Kelas - ' . $kelas->matkul; // Inisialisasi variabel $title
return view('edit', compact('kelas', 'title'));
return view('.kelas.edit', compact('kelas', 'title'));
}


Expand Down
80 changes: 80 additions & 0 deletions app/Http/Controllers/NoteController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
<?php

namespace App\Http\Controllers;

// app/Http/Controllers/NotesController.php

use App\Models\Note;
use Illuminate\Http\Request;
use App\Models\Kelas;

class NoteController extends Controller
{
public function index()
{
$notes = Note::all(); // Mengambil semua data catatan dari database

return view('notes.index', [
'title' => 'Catatan Kuliah',
'notes' => $notes, // Meneruskan data catatan ke tampilan
]);
}



public function store(Request $request)
{
// Validasi data input dari form
$validatedData = $request->validate([
'judul' => 'required',
'isi' => 'required',
'kelas_id' => 'required',
]);

// Simpan catatan baru ke database
Note::create($validatedData);

return redirect('/notes')->with('success', 'Catatan berhasil ditambahkan.');
}

public function create()
{
$kelas = Kelas::all(); // Mengambil data mata kuliah (kelas) dari tabel kelas

return view('notes.create', [
'title' => 'Tambah Catatan Baru',
'kelas' => $kelas, // Meneruskan data mata kuliah ke tampilan create
]);

}



public function edit(Note $note)
{
// Ambil data kelas yang diperlukan
$kelas = Note::all(); // Gantilah dengan metode yang sesuai untuk mengambil data kelas

return view('notes.edit', compact('note', 'kelas'));
}


public function update(Request $request, Note $note)
{
$validatedData = $request->validate([
'judul' => 'required',
'isi' => 'required',
'kelas_id' => 'required',
]);

$note->update($validatedData);

return redirect('/notes')->with('success', 'Catatan berhasil diperbarui.');
}

public function destroy(Note $note)
{
$note->delete();
return redirect('/notes')->with('success', 'Catatan berhasil dihapus.');
}
}
5 changes: 5 additions & 0 deletions app/Models/Kelas.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ class Kelas extends Model
{
use HasFactory;

public function notes()
{
return $this->hasMany(Note::class);
}

protected $fillable = ["matkul", "image", "status"];

protected $table = 'kelas';
Expand Down
25 changes: 25 additions & 0 deletions app/Models/Note.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use App\Models\Kelas;


class Note extends Model
{
use HasFactory;

protected $table = 'notes';

protected $fillable = ["judul", "isi", "kelas_id"];

public function kelas()
{
return $this->belongsTo(Kelas::class);
}

public $timestamps = false;

}
35 changes: 35 additions & 0 deletions database/migrations/2023_10_21_112320_create_notes_table.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateNotesTable extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('notes', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('kelas_id');
$table->string('judul');
$table->text('isi');

$table->foreign('kelas_id')->references('id')->on('kelas')->onDelete('cascade');
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('notes');
}
}
Binary file added public/images/game.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
76 changes: 15 additions & 61 deletions resources/views/Notes.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,25 @@

@section('container')
<h1>Catatan Kuliah</h1>
<a href="/NewNotes" class="button-link">Tambah</a>
<div class="d-flex flex-wrap mt-3">
<div class="card m-1" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">PBKK</h5>
<h6 class="card-subtitle mb-2 text-muted">Pertemuan 3</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="card-link">Buka</a>
</div>
</div>
<div class="card m-1" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Sistem Terdistribusi</h5>
<h6 class="card-subtitle mb-2 text-muted">Pertemuan 1</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="card-link">Buka</a>
</div>
</div>
<div class="card m-1" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Sistem Terdistribusi</h5>
<h6 class="card-subtitle mb-2 text-muted">Pertemuan 2</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="card-link">Buka</a>
</div>
</div>
<div class="card m-1" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Riset Operasi</h5>
<h6 class="card-subtitle mb-2 text-muted">Pertemuan 5</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="card-link">Buka</a>
</div>
</div>
<div class="card m-1" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Mesin Pembelajar</h5>
<h6 class="card-subtitle mb-2 text-muted">Pertemuan 2</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="card-link">Buka</a>
</div>
</div>
<div class="card m-1" style="width: 18rem;">
<div class="card-body">
<h5 class="card-title">Etika Profesi</h5>
<h6 class="card-subtitle mb-2 text-muted">Pertemuan 1</h6>
<p class="card-text">Some quick example text to build on the card title and make up the bulk of the card's content.</p>
<a href="#" class="card-link">Buka</a>
</div>
</div>
</div>
<a href="{{ route('notes.create') }}" class="btn btn-primary mb-3">Tambah</a>



<style>
h1{
h1 {
font-weight: bold;
text-align: center;
}
.button-link {
display: inline-block;
padding: 5px 14px;
background-color: #333333; /* Warna latar belakang */
color: #fff; /* Warna teks */
text-decoration: none; /* Hilangkan garis bawah tautan */
border: none; /* Hilangkan batas */
border-radius: 5px; /* Sudut melengkung */
cursor: pointer;
}
display: inline-block;
padding: 5px 14px;
background-color: #333333; /* Warna latar belakang */
color: #fff; /* Warna teks */
text-decoration: none; /* Hilangkan garis bawah tautan */
border: none; /* Hilangkan batas */
border-radius: 5px; /* Sudut melengkung */
cursor: pointer;
}
</style>
@endsection
File renamed without changes.
15 changes: 9 additions & 6 deletions resources/views/kelas/show.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@

@section('container')
<div class="container">
<h1 class="mb-4">{{ $kelas->matkul }}</h1>
<h1>{{ $kelas->matkul }}</h1>
<img src="{{ asset('storage/' . $kelas->image) }}" alt="{{ $kelas->matkul }}" class="img-fluid mb-4">
<p class="text-muted">Status: {{ $kelas->status }}</p>

<img src="{{ asset('storage/' . $kelas->image) }}" alt="{{ $kelas->matkul }}" class="img-fluid mb-4">

<p class="text-muted">Status: {{ $kelas->status }}</p>

<!-- Tampilkan konten atau detail lainnya sesuai kebutuhan -->
<h2>Catatan</h2>
<ul>
@foreach ($kelas->notes as $note)
<li>{{ $note->judul }} - {{ $note->isi }}</li>
@endforeach
</ul>
</div>
@endsection
3 changes: 2 additions & 1 deletion resources/views/layouts/main.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
<script src='https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.min.js'></script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/fullcalendar/3.10.2/fullcalendar.min.js'></script>

<title>UniverSaya | {{ $title }}</title>
<title>UniverSaya</title>
<link rel="icon" href="{{ asset('images/game.png') }}" type="image/png">
</head>
<body>

Expand Down
38 changes: 38 additions & 0 deletions resources/views/notes/create.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
@extends('layouts.main')

@section('container')
<div class="container">
<h1>Tambah Catatan</h1>
<form action="{{ route('notes.store') }}" method="POST" enctype="multipart/form-data">
@csrf

<div class="form-group">
<label for="judul" class="form-label">Judul</label>
<input type="text" class="form-control" id="judul" name="judul" required>
</div>
<div class="form-group">
<label for="isi" class="form-label">Isi</label>
<textarea class="form-control" id="isi" name="isi" rows="5" required></textarea>
</div>
<div class="form-group">
<label for="kelas_id" class="form-label">Mata Kuliah</label>
<select class="form-select" id="kelas_id" name="kelas_id" required>
@if (!empty($kelas) && is_array($kelas))
@if (count($kelas) > 0)
@foreach ($kelas as $kelasItem)
<option value="{{ $kelasItem->id }}">{{ $kelasItem->matkul }}</option>
@endforeach
@else
<option value="">Pilih Mata Kuliah</option>
@endif
@else
<option value="">Pilih Mata Kuliah</option>
<p>Tidak ada data kelas yang tersedia.</p>
@endif
</select>
</div>

<button type="submit" class="btn btn-primary mt-3">Simpan</button>
</form>
</div>
@endsection
27 changes: 27 additions & 0 deletions resources/views/notes/edit.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@extends('layouts.main')

@section('container')
<h1>Edit Catatan</h1>

<form action="{{ route('notes.update', $note) }}" method="POST">
@csrf
@method('PUT')
<div class="mb-3">
<label for="judul" class="form-label">Judul</label>
<input type="text" class="form-control" id="judul" name="judul" value="{{ $note->judul }}" required>
</div>
<div class="mb-3">
<label for="isi" class="form-label">Isi</label>
<textarea class="form-control" id="isi" name="isi" rows="5" required>{{ $note->isi }}</textarea>
</div>
<div class="mb-3">
<label for="kelas_id" class="form-label">Mata Kuliah</label>
<select class="form-select" id="kelas_id" name="kelas_id" required>
@foreach ($kelas as $kelas)
<option value="{{ $kelas->id }}" @if($note->kelas_id == $kelas->id) selected @endif>{{ $kelas->matkul }}</option>
@endforeach
</select>
</div>
<button type="submit" class="btn btn-primary">Simpan Perubahan</button>
</form>
@endsection
Loading

0 comments on commit 9352d41

Please sign in to comment.