Skip to content

Instantly share code, notes, and snippets.

@herusdianto
Last active August 29, 2015 14:08
Show Gist options
  • Save herusdianto/5bbc2b60052c1c0d15b7 to your computer and use it in GitHub Desktop.
Save herusdianto/5bbc2b60052c1c0d15b7 to your computer and use it in GitHub Desktop.
Laravel Eloquent Relationship: Many To Many
<?php
// app/database/seeds/DatabaseSeeder.php
/**
* Class DatabaseSeeder
*/
class DatabaseSeeder extends Seeder {
/**
* Run the database seeds.
*
* @return void
*/
public function run()
{
Eloquent::unguard();
DB::statement('SET FOREIGN_KEY_CHECKS = 0'); // disable foreign key constraints
$this->call('TabelMahasiswaSeeder');
$this->call('TabelMatakuliahSeeder');
$this->call('TabelMahasiswaMatakuliahSeeder');
DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // enable foreign key constraints
}
}
<?php
// app/models/Mahasiswa.php
/**
* Class Mahasiswa
*/
class Mahasiswa extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'mahasiswa';
/**
* set timestamps to false
*
* @var boolean
*/
public $timestamps = false;
/**
* relasi dengan tabel matakuliah
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function matakuliah()
{
return $this->belongsToMany('Matakuliah', 'mahasiswa_matakuliah', 'id_mahasiswa', 'id_matakuliah');
}
}
<?php
// app/models/MahasiswaMatakuliah.php
/**
* Class MahasiswaMatakuliah
*/
class MahasiswaMatakuliah extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'mahasiswa_matakuliah';
/**
* set timestamps to false
*
* @var boolean
*/
public $timestamps = false;
}
<?php
// app/models/Matakuliah.php
/**
* Class Matakuliah
*/
class Matakuliah extends Eloquent {
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'matakuliah';
/**
* set timestamps to false
*
* @var boolean
*/
public $timestamps = false;
/**
* relasi dengan tabel mahasiswa
*
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
*/
public function mahasiswa()
{
return $this->belongsToMany('Mahasiswa', 'mahasiswa_matakuliah', 'id_matakuliah', 'id_mahasiswa');
}
}
<?php
// app/database/migrations/tabel_mahasiswa.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class TabelMahasiswa
*/
class TabelMahasiswa extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('mahasiswa', function (Blueprint $table)
{
$table->increments('id');
$table->string('nama');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('mahasiswa');
}
}
<?php
// app/database/migrations/tabel_mahasiswa_matakuliah.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class TabelMahasiswaMatakuliah
*/
class TabelMahasiswaMatakuliah extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('mahasiswa_matakuliah', function (Blueprint $table)
{
$table->increments('id');
$table->integer('id_mahasiswa')
->unsigned();
$table->integer('id_matakuliah')
->unsigned();
$table->foreign('id_mahasiswa')
->references('id')
->on('mahasiswa')
->onDelete('cascade');
$table->foreign('id_matakuliah')
->references('id')
->on('matakuliah')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('mahasiswa_matakuliah');
}
}
<?php
// app/database/migrations/tabel_matakuliah.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class TabelMatakuliah
*/
class TabelMatakuliah extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('matakuliah', function (Blueprint $table)
{
$table->increments('id');
$table->string('nama');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('matakuliah');
}
}
<?php
// app/database/seeds/TabelMahasiswaMatakuliahSeeder.php
/**
* Class TabelMahasiswaMatakuliahSeeder
*/
class TabelMahasiswaMatakuliahSeeder extends Seeder {
public function run()
{
MahasiswaMatakuliah::truncate();
MahasiswaMatakuliah::create([
'id_mahasiswa' => 1,
'id_matakuliah' => 1
]);
MahasiswaMatakuliah::create([
'id_mahasiswa' => 1,
'id_matakuliah' => 2
]);
MahasiswaMatakuliah::create([
'id_mahasiswa' => 2,
'id_matakuliah' => 1
]);
MahasiswaMatakuliah::create([
'id_mahasiswa' => 2,
'id_matakuliah' => 2
]);
MahasiswaMatakuliah::create([
'id_mahasiswa' => 2,
'id_matakuliah' => 3
]);
}
}
<?php
// app/database/seeds/TabelMahaSiswaSeeder.php
/**
* Class TabelMahasiswaSeeder
*/
class TabelMahasiswaSeeder extends Seeder {
public function run()
{
Mahasiswa::truncate();
Mahasiswa::create([
'nama' => 'Heru Rusdianto'
]);
Mahasiswa::create([
'nama' => 'Ibnu Rusdianto'
]);
}
}
<?php
// app/database/seeds/TabelMatakuliahSeeder.php
/**
* Class TabelMatakuliahSeeder
*/
class TabelMatakuliahSeeder extends Seeder {
public function run()
{
Matakuliah::truncate();
Matakuliah::create([
'nama' => 'HTML'
]);
Matakuliah::create([
'nama' => 'PHP'
]);
Matakuliah::create([
'nama' => 'JAVA'
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment