Skip to content

Instantly share code, notes, and snippets.

@herusdianto
Created November 9, 2014 07:14
Show Gist options
  • Save herusdianto/37b58d125d5c99e25e96 to your computer and use it in GitHub Desktop.
Save herusdianto/37b58d125d5c99e25e96 to your computer and use it in GitHub Desktop.
Laravel Eloquent Relationship: Many To Many 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 tags
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function tags()
{
return $this->morphToMany('Tag', 'taggable', 'taggables', 'taggable_id', 'id_tag');
}
}
<?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('TabelTagsSeeder');
$this->call('TabelTaggableSeeder');
DB::statement('SET FOREIGN_KEY_CHECKS = 1'); // enable foreign key constraints
}
}
<?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 tags
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function tags()
{
return $this->morphToMany('Tag', 'taggable', 'taggables', 'taggable_id', 'id_tag');
}
}
<?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_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/migrations/tabel_taggables.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class TabelTaggables
*/
class TabelTaggables extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('taggables', function (Blueprint $table)
{
$table->increments('id');
$table->integer('id_tag')->unsigned();;
$table->integer('taggable_id');
$table->string('taggable_type');
$table->foreign('id_tag')
->references('id')
->on('tags')
->onDelete('cascade');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('taggables');
}
}
<?php
// app/database/migrations/tabel_tags.php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
/**
* Class TabelTags
*/
class TabelTags extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('tags', function (Blueprint $table)
{
$table->increments('id');
$table->string('nama');
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('tags');
}
}
<?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/TabelStatusSeeder.php
/**
* Class TabelStatusSeeder
*/
class TabelStatusSeeder extends Seeder {
public function run()
{
Status::truncate();
Status::create([
'isi' => 'Isi status.'
]);
}
}
<?php
// app/database/seeds/TabelTaggableSeeder.php
/**
* Class TabelTaggableSeeder
*/
class TabelTaggableSeeder extends Seeder {
public function run()
{
Taggable::truncate();
Taggable::create([
'id_tag' => 1,
'taggable_id' => 1,
'taggable_type' => 'Status'
]);
Taggable::create([
'id_tag' => 1,
'taggable_id' => 1,
'taggable_type' => 'Aktivitas'
]);
Taggable::create([
'id_tag' => 2,
'taggable_id' => 1,
'taggable_type' => 'Aktivitas'
]);
}
}
<?php
// app/database/seeds/TabelTagsSeeder.php
/**
* Class TabelTagsSeeder
*/
class TabelTagsSeeder extends Seeder {
public function run()
{
Tag::truncate();
Tag::create([
'nama' => 'Tag A.'
]);
Tag::create([
'nama' => 'Tag B.'
]);
}
}
<?php
// app/models/Tag.php
/**
* Class Tag
*/
class Tag extends Eloquent {
/**
* set timestamps to false
*
* @var boolean
*/
public $timestamps = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'tags';
/**
* relasi dengan tabel status
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function status()
{
return $this->morphedByMany('Status', 'taggable', 'taggables', 'taggable_id', 'id_tag');
}
/**
* relasi dengan tabel aktivitas
*
* @return \Illuminate\Database\Eloquent\Relations\MorphToMany
*/
public function aktivitas()
{
return $this->morphedByMany('Aktivitas', 'taggable', 'taggables', 'taggable_id', 'id_tag');
}
}
<?php
// app/models/Taggable.php
/**
* Class Taggable
*/
class Taggable extends Eloquent {
/**
* set timestamps to false
*
* @var boolean
*/
public $timestamps = false;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'taggables';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment