Professional Documents
Culture Documents
Belajar Laravel 14-Nested Relationship
Belajar Laravel 14-Nested Relationship
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat dan Mengisi Table Teachers
.php artisan make:migration create_teachers_table (Membuat Table Harus Plural "-s")
-----------------------------------------------------------------------------------
-----------------------------------------
*Buka File Table Teachers
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::dropIfExists('teachers');
}
};
-----------------------------------------------------------------------------------
---------------------------------------
Mengisi Table Teachers Di phpMyAdmin Dengan Cara Insert Data
-----------------------------------------------------------------------------------
-----------------------------------------
Jalankan Perintah : .php artisan migrate
-----------------------------------------------------------------------------------
-----------------------------------------
Mengisi Table Teachers Di phpMyAdmin Dengan Cara Insert Data
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat Route Teacher Di Web.php
.Route::get('/teacher',[TeacherController::class, 'index']);
-----------------------------------------------------------------------------------
----------------------------------------
*Membuat Halaman Baru Di Views->layouts->mainlayout.blade.php
<li class="nav-item">
<a class="nav-link" href="/teacher">Teacher</a>
</li>
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat Controller View Teacher
.php artisan make:controller TeacherController
*Buka file TeachherController
<?php
namespace App\Http\Controllers;
use App\Models\Teacher;
use Illuminate\Http\Request;
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
@section('title', 'Teacher')
@section('content')
<h1>Ini Halaman Teacher</h1>
<h3>List Teacher</h3>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Teacher</th>
</tr>
</thead>
<tbody>
{{-- mengambil teacherList dari Controller --}}
@foreach ($teacherList as $item)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$item->name}}</td>
</tr>
@endforeach
</tbody>
</table>
@endsection
===================================================================================
==========================================
*Edit Table Class & Menambah Data Ke teacher_id
===================================================================================
==========================================
*Membuat migration add_teacher_id_to_column
.php artisan make:migration add_teacher_id_column_to_class_table
-----------------------------------------------------------------------------------
-----------------------------------------
Buka Filenya:
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('class', function (Blueprint $table) {
$table->dropForeign(['teacher_id']);
$table->dropColumn('teacher_id');
});
}
};
Jalankan Perintah .php artisan migrate untuk mengirim ke dtbase
-----------------------------------------------------------------------------------
-----------------------------------------
Mengisi column teacher_id di phpmyadmin dengan cara insert data
-----------------------------------------------------------------------------------
-----------------------------------------
*Edit Tampilan Class
buka resource->views->classroom.blade.php
@extends('layouts.mainlayout')
@section('title', 'Student')
@section('content')
<h1>Ini Halaman Class</h1>
<h3>List Class</h3>
<table class="table">
<thead>
<tr>
<th>No.</th>
<th>Kelas</th>
<th>Students</th>
<th>Walikelas</th> -><th> baru untuk tampilan table walikelas
</tr>
</thead>
<tbody>
<!-- perulangan foreach ngambil data dari table class -->
{{-- memanggil classList dari Controller Class --}}
@foreach ($classList as $data)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$data->name}}</td>
<td>
@foreach($data->students as $students)
-{{$students->name}} <br>
@endforeach
</td>
<td>{{$data->waliKelas->name}}</td> -><td> baru untuk memanggil
data baru dari waliKelas
</tr>
@endforeach
</tbody>
</table>
@endsection
-----------------------------------------------------------------------------------
-----------------------------------------
*Buka file Model ClassRoom
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
<?php
namespace App\Http\Controllers;
use App\Models\ClassRoom;
use Illuminate\Http\Request;
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
use Doctrine\DBAL\Types\Type;
use Illuminate\Support\Facades\DB;
}
-----------------------------------------------------------------------------------
-----------------------------------------
*Buka file VIEWS student.blade.php
@extends('layouts.mainlayout')
@section('title', 'Students')
@section('content')
<h1>Ini Halaman Student</h1>
<h3>List Students</h3>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Gender</th>
<th>Nis</th>
<th>Class</th>
<th>Extracurricular</th>
<th>Wali Kelas</th> -> <th> baru untuk menampilkan table Wali
Kelas
</thead>
<tbody>
<ol>
<!-- perulangan foreach ngambil data dari table students -->
{{-- memanggil studentList dari Controller Student --}}
@foreach ($studentList as $data)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$data->name}}</td>
<td>{{$data->gender}}</td>
<td>{{$data->nis}}</td>
<td>{{$data->class->name}}</td>
<td>
@foreach ($data->extracurriculars as $item)
- {{$item->name}} <br>
@endforeach
</td>
<td>{{$data->class->waliKelas->name}}</td> -><td> untuk
memanggil class dan funcion/relationship waliKelas dari ClassRoom
</tr>
@endforeach
</ol>
</tbody>
</table>
@endsection
}