You are on page 1of 6

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;

return new class extends Migration


{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('teachers', function (Blueprint $table) {
$table->id();
$table->string('name', 100);
$table->timestamps();
});
}

/**
* 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;

class TeacherController extends Controller


{
public function index()
{
//cara eager loading
$teacher = Teacher::all(); // select * from students;
return view('teacher', ['teacherList'=> $teacher]);
}
}
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat Model Teacher
.php artisan make:model Teacher
-----------------------------------------------------------------------------------
-----------------------------------------
*Buka file modelnya (Teacher.php)
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;

class Teacher extends Model


{
use HasFactory;
}
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat views Teacher (teacher.blade.php)
resource->views->new file
*Buka filenya
@extends('layouts.mainlayout')

@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;

return new class extends Migration


{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('class', function (Blueprint $table) {
$table->unsignedBigInteger('teacher_id')->after('name')-> nullable();
$table->foreign('teacher_id')->references('id')->on('teachers')-
>onDelete('restrict');
});
}

/**
* 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;

class ClassRoom extends Model


{
use HasFactory;

protected $table ='class';


public function students()
{
return $this->hasMany(Student::class, 'class_id', 'id');
}

public function waliKelas() ->Membuat function waliKelas


{
return $this->belongsTo(Teacher::class, 'teacher_id', 'id'); -> tambahkan
belongsTo
}
}
-----------------------------------------------------------------------------------
----------------------------------------
*Buka file Controller ClassController

<?php

namespace App\Http\Controllers;

use App\Models\ClassRoom;
use Illuminate\Http\Request;

class ClassController extends Controller


{
public function index()
{
$class = ClassRoom::with('students','waliKelas')->get(); -> menambah
function waliKelas dari Model
return view('classroom', ['classList'=> $class]);
}
}
===================================================================================
========================================
*Menggunakan Nested Relationship
===================================================================================
========================================
*Buka file controllernya
<?php

namespace App\Http\Controllers;

use App\Models\Student;
use Illuminate\Http\Request;
use Doctrine\DBAL\Types\Type;
use Illuminate\Support\Facades\DB;

class StudentController extends Controller


{
public function index()
{
//cara lazy loading
// $student = Student::all(); // select * from students;
// return view('student', ['studentList'=> $student]);

//cara eager loading


$student = Student::with(['class.waliKelas', 'extracurriculars'])->get(); -
>memasukkan function waliKelas dari Model ClasRoom
return view('student', ['studentList'=> $student]);

}
-----------------------------------------------------------------------------------
-----------------------------------------
*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
}

You might also like