Professional Documents
Culture Documents
Belajar Laravel 12-Eloquent Relationsip
Belajar Laravel 12-Eloquent Relationsip
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;
namespace App\Http\Controllers;
use App\Models\Student;
use Illuminate\Http\Request;
use Doctrine\DBAL\Types\Type;
use Illuminate\Support\Facades\DB;
}
}
-----------------------------------------------------------------------------------
----------------------------------------
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