Skip to content

Commit dc0c777

Browse files
[9.x] Added missing PHPDoc type to Factory's hasAttached first argument (#43373)
* Added missing allowed type to hasAttached first argument * Updated missing type * Updated missing type * Added test * Removed space
1 parent e4f6c3f commit dc0c777

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

src/Illuminate/Database/Eloquent/Factories/BelongsToManyRelationship.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class BelongsToManyRelationship
1010
/**
1111
* The related factory instance.
1212
*
13-
* @var \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model
13+
* @var \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array
1414
*/
1515
protected $factory;
1616

@@ -31,7 +31,7 @@ class BelongsToManyRelationship
3131
/**
3232
* Create a new attached relationship definition.
3333
*
34-
* @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $factory
34+
* @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $factory
3535
* @param callable|array $pivot
3636
* @param string $relationship
3737
* @return void

src/Illuminate/Database/Eloquent/Factories/Factory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,7 +569,7 @@ protected function guessRelationship(string $related)
569569
/**
570570
* Define an attached relationship for the model.
571571
*
572-
* @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model $factory
572+
* @param \Illuminate\Database\Eloquent\Factories\Factory|\Illuminate\Support\Collection|\Illuminate\Database\Eloquent\Model|array $factory
573573
* @param (callable(): array<string, mixed>)|array<string, mixed> $pivot
574574
* @param string|null $relationship
575575
* @return static

tests/Database/DatabaseEloquentFactoryTest.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,29 @@ public function test_belongs_to_many_relationship_with_existing_model_instances(
378378
unset($_SERVER['__test.role.creating-role']);
379379
}
380380

381+
public function test_belongs_to_many_relationship_with_existing_model_instances_using_array()
382+
{
383+
$roles = FactoryTestRoleFactory::times(3)
384+
->afterCreating(function ($role) {
385+
$_SERVER['__test.role.creating-role'] = $role;
386+
})
387+
->create();
388+
FactoryTestUserFactory::times(3)
389+
->hasAttached($roles->toArray(), ['admin' => 'Y'], 'roles')
390+
->create();
391+
392+
$this->assertCount(3, FactoryTestRole::all());
393+
394+
$user = FactoryTestUser::latest()->first();
395+
396+
$this->assertCount(3, $user->roles);
397+
$this->assertSame('Y', $user->roles->first()->pivot->admin);
398+
399+
$this->assertInstanceOf(Eloquent::class, $_SERVER['__test.role.creating-role']);
400+
401+
unset($_SERVER['__test.role.creating-role']);
402+
}
403+
381404
public function test_belongs_to_many_relationship_with_existing_model_instances_with_relationship_name_implied_from_model()
382405
{
383406
$roles = FactoryTestRoleFactory::times(3)

0 commit comments

Comments
 (0)