Created
November 8, 2014 08:21
-
-
Save herusdianto/708e2e8d0969eb4d3764 to your computer and use it in GitHub Desktop.
Laravel Eloquent Relationship: Has Many Through
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('TabelKabupatenSeeder'); | |
$this->call('TabelPendudukSeeder'); | |
$this->call('TabelDesaSeeder'); | |
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/Desa.php | |
/** | |
* Class Desa | |
*/ | |
class Desa extends Eloquent { | |
/** | |
* set timestamps to false | |
* | |
* @var boolean | |
*/ | |
public $timestamps = false; | |
/** | |
* The database table used by the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'desa'; | |
} |
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/Kabupaten.php | |
/** | |
* Class Kabupaten | |
*/ | |
class Kabupaten extends Eloquent { | |
/** | |
* set timestamps to false | |
* | |
* @var boolean | |
*/ | |
public $timestamps = false; | |
/** | |
* The database table used by the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'kabupaten'; | |
/** | |
* relasi dengan tabel desa | |
* | |
* @return \Illuminate\Database\Eloquent\Relations\HasManyThrough | |
*/ | |
public function desa() | |
{ | |
return $this->hasManyThrough('Desa', 'Penduduk', 'id_kabupaten', 'id_penduduk'); | |
} | |
} |
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/Penduduk.php | |
/** | |
* Class Penduduk | |
*/ | |
class Penduduk extends Eloquent { | |
/** | |
* set timestamps to false | |
* | |
* @var boolean | |
*/ | |
public $timestamps = false; | |
/** | |
* The database table used by the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'penduduk'; | |
} |
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_desa.php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
/** | |
* Class TabelDesa | |
*/ | |
class TabelDesa extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('desa', function (Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->integer('id_penduduk') | |
->unsigned(); | |
$table->string('nama'); | |
$table->foreign('id_penduduk') | |
->references('id') | |
->on('penduduk') | |
->onDelete('cascade'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop('desa'); | |
} | |
} |
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_kabupaten.php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
/** | |
* Class TabelKabupaten | |
*/ | |
class TabelKabupaten extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('kabupaten', function (Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->string('nama'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop('kabupaten'); | |
} | |
} |
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_penduduk.php | |
use Illuminate\Database\Migrations\Migration; | |
use Illuminate\Database\Schema\Blueprint; | |
/** | |
* Class TabelPenduduk | |
*/ | |
class TabelPenduduk extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('penduduk', function (Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->integer('id_kabupaten') | |
->unsigned(); | |
$table->string('nama'); | |
$table->foreign('id_kabupaten') | |
->references('id') | |
->on('kabupaten') | |
->onDelete('cascade'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop('penduduk'); | |
} | |
} |
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/TabelDesaSeeder.php | |
/** | |
* Class TabelDesaSeeder | |
*/ | |
class TabelDesaSeeder extends Seeder { | |
public function run() | |
{ | |
Desa::truncate(); | |
Desa::create([ | |
'id_penduduk' => 1, | |
'nama' => 'Cigugur Kidul' | |
]); | |
Desa::create([ | |
'id_penduduk' => 2, | |
'nama' => 'Cigugur Kaler' | |
]); | |
} | |
} |
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/TabelKabupatenSeeder.php | |
/** | |
* Class TabelKabupatenSeeder | |
*/ | |
class TabelKabupatenSeeder extends Seeder { | |
public function run() | |
{ | |
Kabupaten::truncate(); | |
Kabupaten::create([ | |
'nama' => 'Subang' | |
]); | |
} | |
} |
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/TabelPendudukSeeder.php | |
/** | |
* Class TabelPendudukSeeder | |
*/ | |
class TabelPendudukSeeder extends Seeder { | |
public function run() | |
{ | |
Penduduk::truncate(); | |
Penduduk::create([ | |
'id_kabupaten' => 1, | |
'nama' => 'Heru Rusdianto' | |
]); | |
Penduduk::create([ | |
'id_kabupaten' => 1, | |
'nama' => 'Ibnu Rusdianto' | |
]); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment