Last active
August 29, 2015 14:08
-
-
Save herusdianto/5bbc2b60052c1c0d15b7 to your computer and use it in GitHub Desktop.
Laravel Eloquent Relationship: Many To Many
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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'); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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' | |
]); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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