Skip to content

Instantly share code, notes, and snippets.

@herusdianto
Created October 2, 2014 08:57
Show Gist options
  • Save herusdianto/960b2e3311778e2b9235 to your computer and use it in GitHub Desktop.
Save herusdianto/960b2e3311778e2b9235 to your computer and use it in GitHub Desktop.
Laravel Eloquent Relationship: Belongs To
<?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;
/**
* relasi dengan tabel siswa
*
* @return Illuminate\Database\Eloquent\Collection
*/
public function siswa()
{
return $this->belongsTo('Siswa', 'id_siswa', 'id');
}
}
<?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'); // enable foreign key constraints
}
}
<?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;
}
<?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');
}
}
<?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');
}
}
<?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'
]);
}
}
<?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