Created
November 9, 2014 07:14
-
-
Save herusdianto/37b58d125d5c99e25e96 to your computer and use it in GitHub Desktop.
Laravel Eloquent Relationship: Many To Many Polymorphic Relations
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/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'); | |
} | |
} |
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('TabelStatusSeeder'); | |
$this->call('TabelAktivitasSeeder'); | |
$this->call('TabelTagsSeeder'); | |
$this->call('TabelTaggableSeeder'); | |
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/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'); | |
} | |
} |
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_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'); | |
} | |
} |
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_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'); | |
} | |
} |
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_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'); | |
} | |
} |
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/TabelAktivitasSeeder.php | |
/** | |
* Class TabelAktivitasSeeder | |
*/ | |
class TabelAktivitasSeeder extends Seeder { | |
public function run() | |
{ | |
Aktivitas::truncate(); | |
Aktivitas::create([ | |
'isi' => 'Isi aktivitas.' | |
]); | |
} | |
} |
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/TabelStatusSeeder.php | |
/** | |
* Class TabelStatusSeeder | |
*/ | |
class TabelStatusSeeder extends Seeder { | |
public function run() | |
{ | |
Status::truncate(); | |
Status::create([ | |
'isi' => 'Isi status.' | |
]); | |
} | |
} |
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/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' | |
]); | |
} | |
} |
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/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'); | |
} | |
} |
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/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