Belajar Laravel 13-Many To Many Relationship

You might also like

Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 6

===================================================================================

=========================================
Many To Many Relationship
===================================================================================
=========================================
*Membuat Table,Model,Controller Extracurricular
.php artisan make:migration create_extracurriculars_table

*Buka File Migration nya :


<?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('extracurriculars', function (Blueprint $table) {
$table->id();
$table->string('name', 100); -> tambahkan nama
$table->timestamps();
});
}

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

*Buka file Modelnya:


<?php

namespace App\Models;

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

class Exracurricular extends Model


{
use HasFactory;
}
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat Controller Extracurricular
.php artisan make:controller ExtracurricularController

*Buka file Controllernya:


<?php

namespace App\Http\Controllers;

use App\Models\Exracurricular;
use App\Models\Extracurricular;
use Illuminate\Http\Request;

class ExtracurricularController extends Controller


{
public function index()
{
$eskul = Extracurricular::all();
return view('extracurricular', ['eskulList'=> $eskul]);
}
}
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat Route Extracurricular Di Web.php
.Route::get('/extracurricular',[ExtracurricularController::class, 'index']);
-----------------------------------------------------------------------------------
----------------------------------------
*Membuat Halaman Baru Di Views->layouts->mainlayout.blade.php
<li class="nav-item">
<a class="nav-link" href="/extracurricular">Extracurricular</a>
</li>
-----------------------------------------------------------------------------------
-----------------------------------------
*Membuat File Baru Di Views extracurricular.blade.php
<!-- Layouts Using Template Inheritance -->
@extends('layouts.mainlayout')

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

return new class extends Migration


{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('student_extracurricular', function (Blueprint $table) {
$table->unsignedBigInteger('student_id');
$table->foreign('student_id')->references('id')->on('students') -
>onDelete('restrict');
$table->unsignedBigInteger('extracurricular_id');
$table->foreign('extracurricular_id')->references('id')-
>on('extracurriculars') ->onDelete('restrict');
});
}

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

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);
}

public function extracurriculars()


{
return $this->belongsToMany(Extracurricular::class,
'student_extracurricular', 'student_id', 'extracurricular_id');
}
-----------------------------------------------------------------------------------
-----------------------------------------
*Panggil Di Controller Student (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 eager loading
$student = Student::with(['class', 'extracurriculars'])->get(); //
memanggil class dan extracurricular dari Model
return view('student', ['studentList'=> $student]);
}
}
}
-----------------------------------------------------------------------------------
----------------------------------------
*Buka File Views Student (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> ->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;

class Extracurricular extends Model


{
use HasFactory;
public function students()
{
return $this->belongsToMany(Student::class,
'student_extracurricular', 'extracurricular_id', 'student_id');
}
}
-----------------------------------------------------------------------------------
-----------------------------------------
*Panggil Di Controller Extracurricular (ExtracurricularController.php)
<?php

namespace App\Http\Controllers;

use App\Models\Exracurricular;
use App\Models\Extracurricular;
use Illuminate\Http\Request;

class ExtracurricularController extends Controller


{
public function index()
{
$eskul = Extracurricular::with('students')->get(); -> memanggil students
dari model
return view('extracurricular', ['eskulList'=> $eskul]);
}
}
-----------------------------------------------------------------------------------
----------------------------------------
*Buka File Views Extracurricular(extracurricular.blade.php)
@extends('layouts.mainlayout')

@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

You might also like