Professional Documents
Culture Documents
Belajar Laravel 13-Many To Many Relationship
Belajar Laravel 13-Many To Many Relationship
Belajar Laravel 13-Many To Many Relationship
=========================================
Many To Many Relationship
===================================================================================
=========================================
*Membuat Table,Model,Controller Extracurricular
.php artisan make:migration create_extracurriculars_table
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('extracurriculars');
}
};
-----------------------------------------------------------------------------------
-----------------------------------------
Jika Sudah Membuat Data jalankan .php artisan migrate untuk mengirim table ke
database
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat Model Extracurricular
.php artisan make:model Extracurricular
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
namespace App\Http\Controllers;
use App\Models\Exracurricular;
use App\Models\Extracurricular;
use Illuminate\Http\Request;
@section('title', 'Extracurricular')
@section('content')
<h1>Ini Halaman Extracurricular</h1>
<h3>List Eskul</h3>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<ol>
<!-- perulangan foreach ngambil data dari table extracurriculars --
>
@foreach ($eskulList as $data)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$data->name}}</td>
</tr>
@endforeach
</ol>
</tbody>
</table>
@endsection
===================================================================================
=========================================
*Membuat Table Pivot (untuk student_id & extrcurricular_id)
.php artisan make:migration create_student_extracurricular_table
-----------------------------------------------------------------------------------
-----------------------------------------
*Buka File Table Pivotnya:
<?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('student_extracurricular');
}
};
-----------------------------------------------------------------------------------
-----------------------------------------
Jalankan Dengan Menggunakan Perintah
.php artisan migrate
===================================================================================
=========================================
Membuat Many to Many Relationship Student
*Buka File Model Student (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;
@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> ->tambah <th> untuk menampilkan table
extracurricular
</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>
@foreach ($data->extracurriculars as $item) -> <td> baru
untuk memanggil data dari table extracurricular
- {{$item->name}} <br>
@endforeach
</td>
</tr>
@endforeach
</ol>
</tbody>
</table>
@endsection
===================================================================================
=========================================
Membuat Many to Many Relationship Extracurricular
*Buka File Model Extracurricular
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
namespace App\Http\Controllers;
use App\Models\Exracurricular;
use App\Models\Extracurricular;
use Illuminate\Http\Request;
@section('title', 'Extracurricular')
@section('content')
<h1>Ini Halaman Extracurricular</h1>
<h3>List Extracurricular</h3>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Extracurricular</th>
<th>Anggota</th> -> <th> baru untuk menampilkan table Anggota
</tr>
</thead>
<tbody>
<ol>
<!-- perulangan foreach ngambil data dari table extracurriculars --
>
@foreach ($eskulList as $data)
<tr>
<td>{{$loop->iteration}}</td>
<td>{{$data->name}}</td>
<td>
@foreach ($data->students as $item) -><td> baru dan foreach
untuk memanggil data dari table students
- {{$item->name}} <br>
@endforeach
</td>
</tr>
@endforeach
</ol>
</tbody>
</table>
@endsection