Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions config/models.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',

/*
|--------------------------------------------------------------------------
Expand Down
1 change: 1 addition & 0 deletions src/Coders/Model/Relations/BelongsTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
12 changes: 12 additions & 0 deletions src/Coders/Model/Relations/HasMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion src/Coders/Model/Relations/RelationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}