Skip to content

Instantly share code, notes, and snippets.

@herusdianto
Created November 8, 2014 09:23
Show Gist options
  • Save herusdianto/ef1d8cf71555c4c00fe7 to your computer and use it in GitHub Desktop.
Save herusdianto/ef1d8cf71555c4c00fe7 to your computer and use it in GitHub Desktop.
Laravel Eloquent Relationship: Polymorphic Relations
<?php
// app/models/Aktivitas.php
/**
* Class Aktivitas
*/
class Aktivitas extends Eloquent {
/**
* set timestamps to false
*
* @var boolean
*/
public $timestamps = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'aktivitas';
/**
* relasi dengan tabel komentar
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function komentar()
{
return $this->morphMany('Komentar', 'commentable');
}
}
<?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('TabelStatusSeeder');
$this->call('TabelAktivitasSeeder');
$this->call('TabelKomentarSeeder');
DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // enable foreign key constraints
}
}
<?php
// app/models/Komentar.php
/**
* Class Komentar
*/
class Komentar extends Eloquent {
/**
* set timestamps to false
*
* @var boolean
*/
public $timestamps = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'komentar';
/**
* relasi dengan tabel status & aktivitas
*
* @return \Illuminate\Database\Eloquent\Relations\MorphTo
*/
public function commentable()
{
return $this->morphTo();
}
}
<?php
// app/models/Status.php
/**
* Class Status
*/
class Status extends Eloquent {
/**
* set timestamps to false
*
* @var boolean
*/
public $timestamps = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'status';
/**
* relasi dengan tabel komentar
*
* @return \Illuminate\Database\Eloquent\Relations\MorphMany
*/
public function komentar()
{
return $this->morphMany('Komentar', 'commentable');
}
}
<?php
// app/database/migrations/tabel_aktivitas.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class TabelAktivitas
*/
class TabelAktivitas extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('aktivitas', function (Blueprint $table)
{
$table->increments('id');
$table->string('isi');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('aktivitas');
}
}
<?php
// app/database/migrations/tabel_komentar.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class TabelKomentar
*/
class TabelKomentar extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('komentar', function (Blueprint $table)
{
$table->increments('id');
$table->string('isi');
$table->integer('commentable_id');
$table->string('commentable_type');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('komentar');
}
}
<?php
// app/database/migrations/tabel_status.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class TabelStatus
*/
class TabelStatus extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('status', function (Blueprint $table)
{
$table->increments('id');
$table->string('isi');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('status');
}
}
<?php
// app/database/seeds/TabelAktivitasSeeder.php
/**
* Class TabelAktivitasSeeder
*/
class TabelAktivitasSeeder extends Seeder {
public function run()
{
Aktivitas::truncate();
Aktivitas::create([
'isi' => 'Isi aktivitas.'
]);
}
}
<?php
// app/database/seeds/TabelKomentarSeeder.php
/**
* Class TabelKomentarSeeder
*/
class TabelKomentarSeeder extends Seeder {
public function run()
{
Komentar::truncate();
Komentar::create([
'isi' => 'Isi komentar status.',
'commentable_id' => 1,
'commentable_type' => 'Status'
]);
Komentar::create([
'isi' => 'Isi komentar aktivitas.',
'commentable_id' => 1,
'commentable_type' => 'Aktivitas'
]);
}
}
<?php
// app/database/seeds/TabelStatusSeeder.php
/**
* Class TabelStatusSeeder
*/
class TabelStatusSeeder extends Seeder {
public function run()
{
Status::truncate();
Status::create([
'isi' => 'Isi status.'
]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment