diff --git a/config/models.php b/config/models.php index 9561e428..97f47308 100644 --- a/config/models.php +++ b/config/models.php @@ -346,10 +346,25 @@ | Where the foreign key matches the related table name, it behaves as per the 'related' strategy. | (post.user_id --> user.id) | generates Post::user() and User::posts() + | + | 'foreign_key_field_name' Use the foreign key as the relation name without its parent table name. + | This can help to provide more meaningful relationship names, and avoids naming conflicts + | if you have more than one relationship between two tables. + | (post.author_id --> user.id) + | generates Post::author() and User::authors() + | (post.editor_id --> user.id) + | generates Post::editor() and User::editor() + | ID suffixes can be omitted from foreign keys. + | (post.author --> user.id) + | (post.editor --> user.id) + | generates the same as above. + | Where the foreign key matches the related table name, it behaves as per the 'related' strategy. + | (post.user_id --> user.id) + | generates Post::user() and User::posts() */ - - 'relation_name_strategy' => 'related', + // 'relation_name_strategy' => 'related', // 'relation_name_strategy' => 'foreign_key', + 'relation_name_strategy' => 'foreign_key_field_name', /* |-------------------------------------------------------------------------- diff --git a/src/Coders/Model/Relations/BelongsTo.php b/src/Coders/Model/Relations/BelongsTo.php index 5c6cf3e1..498b3d69 100644 --- a/src/Coders/Model/Relations/BelongsTo.php +++ b/src/Coders/Model/Relations/BelongsTo.php @@ -51,6 +51,7 @@ public function name() { switch ($this->parent->getRelationNameStrategy()) { case 'foreign_key': + case 'foreign_key_field_name': $relationName = RelationHelper::stripSuffixFromForeignKey( $this->parent->usesSnakeAttributes(), $this->otherKey(), diff --git a/src/Coders/Model/Relations/HasMany.php b/src/Coders/Model/Relations/HasMany.php index ff8d451f..231535c5 100644 --- a/src/Coders/Model/Relations/HasMany.php +++ b/src/Coders/Model/Relations/HasMany.php @@ -26,6 +26,18 @@ public function hint() public function name() { switch ($this->parent->getRelationNameStrategy()) { + case 'foreign_key_field_name': + $relationName = RelationHelper::stripSuffixFromForeignKey( + $this->parent->usesSnakeAttributes(), + $this->localKey(), + $this->foreignKey() + ); + if (Str::snake($relationName) === Str::snake($this->parent->getClassName())) { + $relationName = Str::plural($this->related->getClassName()); + } else { + $relationName = ucfirst(Str::singular($relationName)).Str::plural($this->related->getClassName()); + } + break; case 'foreign_key': $relationName = RelationHelper::stripSuffixFromForeignKey( $this->parent->usesSnakeAttributes(), diff --git a/src/Coders/Model/Relations/RelationHelper.php b/src/Coders/Model/Relations/RelationHelper.php index 08006d7d..c8409046 100644 --- a/src/Coders/Model/Relations/RelationHelper.php +++ b/src/Coders/Model/Relations/RelationHelper.php @@ -24,7 +24,7 @@ public static function stripSuffixFromForeignKey($usesSnakeAttributes, $primaryK return preg_replace('/(_)(' . $primaryKey . '|' . $lowerPrimaryKey . ')$/', '', $foreignKey); } else { $studlyPrimaryKey = Str::studly($primaryKey); - return preg_replace('/(' . $primaryKey . '|' . $studlyPrimaryKey . ')$/', '', $foreignKey); + return preg_replace('/(' . $primaryKey . '|' . $studlyPrimaryKey . '|(_)'. $primaryKey.')$/', '', $foreignKey); } } }