You are on page 1of 2

*Eloquent Relationship

1. student -> class (many to one) banyak student bisa dpet 1 class
2. class -> student (one to many) 1 class bisa dpet banyak student
===================================================================================
========================================
*Many to One Relationship
-----------------------------------------------------------------------------------
----------------------------------------
PERTAMA BUKA FILE MODEL YAITU (Student.php)
-----------------------------------------------------------------------------------
----------------------------------------
-> buka file Model (Student.php)
<?php
// MODEL-> VIEW-> CONTROLLER

namespace App\Models;

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

class Student extends Model


{
use HasFactory;
public function class()
{
// return $this->belongsTo(ClassRoom::class, 'contoh_id', 'id');
// -> jika nama column foreignKey bukan nama table reference harus masukin
nama column foreignKey dan localKey
return $this->belongsTo(ClassRoom::class); -> Memakai belongsTo lalu
memanggil model class (ClassRoom)
}
}
-----------------------------------------------------------------------------------
----------------------------------------
JIKA SUDAH BERHUBUNGAN ANTARA STUDENT DENGAN CLASS BUKA FILE CONTROLLER
(StudentController.php)
-----------------------------------------------------------------------------------
----------------------------------------
-> File StudentController.php
<?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 -> rekomendasi


$student = Student::with('class')->get(); //tambahkan "with('class')-
>get();"
return view('student', ['studentList'=> $student]);

}
}
-----------------------------------------------------------------------------------
----------------------------------------
Jika Sudah Menggunakan Eager Loading, Buka File view yaitu (student.blade.php)
-----------------------------------------------------------------------------------
----------------------------------------
-> File student.blade.php
<!-- Layouts Using Template Inheritance -->
@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 baru untuk menampilkan table class
</tr>
</thead>
<tbody>
<ol>
<!-- perulangan foreach ngambil data dari table students -->
@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 baru untuk memanggil
class->name
</tr>
@endforeach
</ol>
</tbody>
</table>

@endsection

You might also like