Skip to content
Merged
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
4 changes: 2 additions & 2 deletions src/Actions/WriteRelationship.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function __invoke(array $relation, string $indent = '', bool $jsonOutput
$relatedModel = $this->getClassName($relation['related']);

$relationType = match ($relation['type']) {
'BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphToMany', 'MorphMany', 'MorphedByMany' => $plurals === true ? Str::plural($relatedModel) : (Str::singular($relatedModel) . '[]'),
'BelongsTo', 'HasOne', 'HasOneThrough', 'MorphOne', 'MorphTo' => Str::singular($relatedModel),
'BelongsToMany', 'HasMany', 'HasManyThrough', 'MorphToMany', 'MorphMany', 'MorphedByMany' => $plurals === true ? Str::plural($relatedModel) : ($relatedModel . '[]'),
'BelongsTo', 'HasOne', 'HasOneThrough', 'MorphOne', 'MorphTo' => $relatedModel,
default => $relatedModel,
};
}
Expand Down
27 changes: 27 additions & 0 deletions test/Tests/Feature/Actions/WriteRelationshipTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,33 @@ public function test_action_can_return_nullable_plural_relationships()
$this->assertStringContainsString('tags: Tag[] | null', $result);
}

public function test_plural_class_name_is_not_singularized_in_array_relation(): void
{
$relation = [
'name' => 'customer_tests',
'type' => 'HasMany',
'related' => 'App\Models\CustomerTests',
];

$result = app(WriteRelationship::class)($relation);

$this->assertStringContainsString('customer_tests: CustomerTests[]', $result);
$this->assertStringNotContainsString('CustomerTest[]', $result);
}

public function test_plural_class_name_is_not_singularized_in_singular_relation(): void
{
$relation = [
'name' => 'primary_address',
'type' => 'BelongsTo',
'related' => 'App\Models\UserAddresses',
];

$result = app(WriteRelationship::class)($relation);

$this->assertStringContainsString('primary_address: UserAddresses', $result);
}

public function test_action_can_return_morph_to_union_type_relationships()
{
$morphToRelation = [
Expand Down
Loading