Skip to content

Commit

Permalink
Guess inverse relationship name if not provided (#2)
Browse files Browse the repository at this point in the history
Co-authored-by: Alex Bouma <[email protected]>
  • Loading branch information
IonBazan and stayallive authored Apr 22, 2024
1 parent 15a5806 commit c66dce3
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
fail-fast: false
matrix:
php: [ "8.3", "8.2", "8.1", "8.0", "7.4", "7.3" ]
php: [ "8.4", "8.3", "8.2", "8.1", "8.0", "7.4", "7.3" ]

name: phpunit (PHP:${{ matrix.php }})

Expand Down
8 changes: 6 additions & 2 deletions src/HasHasManyWithInverseRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Stayallive\Laravel\Eloquent\Relations;

use Illuminate\Support\Str;

trait HasHasManyWithInverseRelation
{
public function hasManyWithInverse($related, $inverse, $foreignKey = null, $localKey = null): HasManyWithInverseRelation
public function hasManyWithInverse($related, $inverse = null, $foreignKey = null, $localKey = null): HasManyWithInverseRelation
{
/** @var \Illuminate\Database\Eloquent\Model $this */
$instance = $this->newRelatedInstance($related);
$instance = $this->newRelatedInstance($related);

$inverse = $inverse ?: Str::camel(class_basename($this));
$localKey = $localKey ?: $this->getKeyName();
$foreignKey = $foreignKey ?: $this->getForeignKey();

Expand Down
8 changes: 6 additions & 2 deletions src/HasHasOneWithInverseRelation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,16 @@

namespace Stayallive\Laravel\Eloquent\Relations;

use Illuminate\Support\Str;

trait HasHasOneWithInverseRelation
{
public function hasOneWithInverse($related, $inverse, $foreignKey = null, $localKey = null): HasOneWithInverseRelation
public function hasOneWithInverse($related, $inverse = null, $foreignKey = null, $localKey = null): HasOneWithInverseRelation
{
/** @var \Illuminate\Database\Eloquent\Model $this */
$instance = $this->newRelatedInstance($related);
$instance = $this->newRelatedInstance($related);

$inverse = $inverse ?: Str::camel(class_basename($this));
$localKey = $localKey ?: $this->getKeyName();
$foreignKey = $foreignKey ?: $this->getForeignKey();

Expand Down
5 changes: 5 additions & 0 deletions tests/Stubs/HasManyWithInverse/ChildModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public function parent(): BelongsTo
{
return $this->belongsTo(ParentModel::class, 'parent_id');
}

public function parentModel(): BelongsTo
{
return $this->belongsTo(ParentModel::class, 'parent_id');
}
}
6 changes: 6 additions & 0 deletions tests/Stubs/HasManyWithInverse/ParentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* @property int $id
* @property \Illuminate\Database\Eloquent\Collection $children
* @property \Illuminate\Database\Eloquent\Collection $childrenDefaultInverse
*/
class ParentModel extends Model
{
Expand All @@ -24,4 +25,9 @@ public function children(): HasMany
// The `parent` argument (second) is the name of the inverse relationship as defined in the ChildModel
return $this->hasManyWithInverse(ChildModel::class, 'parent', 'parent_id');
}

public function childrenDefaultInverse(): HasMany
{
return $this->hasManyWithInverse(ChildModel::class, null, 'parent_id');
}
}
5 changes: 5 additions & 0 deletions tests/Stubs/HasOneWithInverse/ChildModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,9 @@ public function parent(): BelongsTo
{
return $this->belongsTo(ParentModel::class, 'parent_id');
}

public function parentModel(): BelongsTo
{
return $this->belongsTo(ParentModel::class, 'parent_id');
}
}
6 changes: 6 additions & 0 deletions tests/Stubs/HasOneWithInverse/ParentModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
/**
* @property int $id
* @property \Tests\Stubs\HasOneWithInverse\ChildModel $child
* @property \Tests\Stubs\HasOneWithInverse\ChildModel $childDefaultInverse
*/
class ParentModel extends Model
{
Expand All @@ -24,4 +25,9 @@ public function child(): HasOne
// The `parent` argument (second) is the name of the inverse relationship as defined in the ChildModel
return $this->hasOneWithInverse(ChildModel::class, 'parent', 'parent_id');
}

public function childDefaultInverse(): HasOne
{
return $this->hasOneWithInverse(ChildModel::class, null, 'parent_id');
}
}
11 changes: 11 additions & 0 deletions tests/Unit/HasManyWithInverseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
});
});

test('inverse relationship name can be automatically guessed if not provided', function () {
/** @var \Tests\Stubs\HasManyWithInverse\ParentModel $parent */
$parent = ParentModel::create([]);

/** @var \Tests\Stubs\HasManyWithInverse\ChildModel $child */
$child = $parent->childrenDefaultInverse()->create([]);

expect($child->relationLoaded('parentModel'))->toBeTrue();
expect($child->getRelations()['parentModel']->id)->toBe($parent->id);
});

test('children have the parent relationship automatically set when being created', function () {
/** @var \Tests\Stubs\HasManyWithInverse\ParentModel $parent */
$parent = ParentModel::create([]);
Expand Down
11 changes: 11 additions & 0 deletions tests/Unit/HasOneWithInverseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,17 @@
});
});

test('inverse relationship name can be automatically guessed if not provided', function () {
/** @var \Tests\Stubs\HasOneWithInverse\ParentModel $parent */
$parent = ParentModel::create([]);

/** @var \Tests\Stubs\HasOneWithInverse\ChildModel $child */
$child = $parent->childDefaultInverse()->create([]);

expect($child->relationLoaded('parentModel'))->toBeTrue();
expect($child->getRelations()['parentModel']->id)->toBe($parent->id);
});

test('child has the parent relationship automatically set when being created', function () {
/** @var \Tests\Stubs\HasOneWithInverse\ParentModel $parent */
$parent = ParentModel::create([]);
Expand Down

0 comments on commit c66dce3

Please sign in to comment.