Last active
August 29, 2015 14:07
-
-
Save herusdianto/ebb03163120d07ffe86c to your computer and use it in GitHub Desktop.
Laravel Eloquent Relationship: One To One
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/Alamat.php | |
class Alamat extends Eloquent { | |
/** | |
* The database table used by the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'alamat'; | |
/** | |
* 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/database/seeds/DatabaseSeeder.php | |
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('TabelSiswaSeeder'); | |
$this->call('TabelAlamatSeeder'); | |
DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // en |
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/Siswa.php | |
class Siswa extends Eloquent { | |
/** | |
* The database table used by the model. | |
* | |
* @var string | |
*/ | |
protected $table = 'siswa'; | |
/** | |
* set timestamps to false | |
* | |
* @var boolean | |
*/ | |
public $timestamps = false; | |
/** | |
* relasi dengan tabel alamat | |
* | |
* @return Illuminate\Database\Eloquent\Collection | |
*/ | |
public function alamat() | |
{ | |
return $this->hasOne('Alamat', 'id_siswa', 'id'); | |
} | |
} |
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_alamat.php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class TabelAlamat extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('alamat', function(Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->integer('id_siswa')->unsigned(); | |
$table->string('alamat'); | |
$table->foreign('id_siswa')->references('id')->on('siswa')->onDelete('cascade'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop('alamat'); | |
} | |
} |
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_siswa.php | |
use Illuminate\Database\Schema\Blueprint; | |
use Illuminate\Database\Migrations\Migration; | |
class TabelSiswa extends Migration { | |
/** | |
* Run the migrations. | |
* | |
* @return void | |
*/ | |
public function up() | |
{ | |
Schema::create('siswa', function(Blueprint $table) | |
{ | |
$table->increments('id'); | |
$table->string('nama'); | |
}); | |
} | |
/** | |
* Reverse the migrations. | |
* | |
* @return void | |
*/ | |
public function down() | |
{ | |
Schema::drop('siswa'); | |
} | |
} |
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/TabelAlamatSeeder.php | |
class TabelAlamatSeeder extends Seeder { | |
public function run() | |
{ | |
Alamat::truncate(); | |
Alamat::create([ | |
'id_siswa' => 1, | |
'alamat' => 'Cigugur Kidul' | |
]); | |
Alamat::create([ | |
'id_siswa' => 2, | |
'alamat' => '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/TabelSiswaSeeder.php | |
class TabelSiswaSeeder extends Seeder { | |
public function run() | |
{ | |
Siswa::truncate(); | |
Siswa::create(['nama' => 'Heru Rusdianto']); | |
Siswa::create(['nama' => 'Ibnu Rusdianto']); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment