diff --git a/database/migrations/2020_01_01_155000_create_results_table.php b/database/migrations/2020_01_01_155000_create_results_table.php index 8eff008..d84a1fa 100644 --- a/database/migrations/2020_01_01_155000_create_results_table.php +++ b/database/migrations/2020_01_01_155000_create_results_table.php @@ -19,9 +19,8 @@ public function up() $table->string('type')->index(); // Example: 'Organic', 'Local', 'Ads', 'Inline Video' $table->unsignedTinyInteger('position')->index(); - // Result (one or the other) - $table->foreignIdFor(Webpage::class)->nullable(); - $table->foreignIdFor(Place::class)->nullable(); + // resultable_id, resultable_type either Tipoff\Seo\Models\Webpage or Place + $table->morphs('resultable'); // Don't need timestamps since can use created_at timestamp of the ranking class diff --git a/src/Models/Place.php b/src/Models/Place.php index 5f44eda..4836d6f 100644 --- a/src/Models/Place.php +++ b/src/Models/Place.php @@ -14,4 +14,9 @@ class Place extends BaseModel use HasPackageFactory; use HasCreator; use HasUpdater; + + public function results() + { + return $this->morphMany(Result::class, 'resultable'); + } } diff --git a/src/Models/Ranking.php b/src/Models/Ranking.php index 9d2e136..b824abe 100644 --- a/src/Models/Ranking.php +++ b/src/Models/Ranking.php @@ -15,13 +15,8 @@ class Ranking extends BaseModel use HasCreator; use HasUpdater; - public function webpage() + public function results() { - return $this->belongsTo(app('webpage')); - } - - public function places() - { - return $this->belongsTo(app('place')); + return $this->hasMany(Result::class); } } diff --git a/src/Models/Result.php b/src/Models/Result.php new file mode 100644 index 0000000..1b5f465 --- /dev/null +++ b/src/Models/Result.php @@ -0,0 +1,27 @@ +belongsTo(Ranking::class); + } + + public function resultable() + { + return $this->morphTo(); + } +} diff --git a/src/Models/Webpage.php b/src/Models/Webpage.php index 0be1a48..3b3d2c3 100644 --- a/src/Models/Webpage.php +++ b/src/Models/Webpage.php @@ -15,13 +15,13 @@ class Webpage extends BaseModel use HasCreator; use HasUpdater; - public function domain() + public function results() { - return $this->belongsTo(app('domain')); + return $this->morphMany(Result::class, 'resultable'); } - public function rankings() + public function domain() { - return $this->hasMany(app('ranking')); + return $this->belongsTo(app('domain')); } }