Skip to content

Commit 86f8574

Browse files
browner12devfreyAndrewMast
authored
[12.x] use "class-string" type for using pivot model (#55053)
* use "class-string" type for `using` pivot model the `using` property of the `BelongsToMany` class is not just a string, but specifically a class string, so we'll document it as such. helps in places where [static methods are being called on the string](https://github.com/laravel/framework/blob/12.x/src/Illuminate/Database/Eloquent/Relations/MorphToMany.php#L160) * Update src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php apparently can't use Traits as types Co-authored-by: Jeffrey Angenent <[email protected]> * fixed docblocks for generic to pass type test --------- Co-authored-by: Jeffrey Angenent <[email protected]> Co-authored-by: Andrew Mast <[email protected]>
1 parent eac931d commit 86f8574

File tree

4 files changed

+10
-9
lines changed

4 files changed

+10
-9
lines changed

src/Illuminate/Database/Eloquent/Concerns/HasRelationships.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -564,7 +564,7 @@ protected function newMorphMany(Builder $query, Model $parent, $type, $id, $loca
564564
* @param string|null $parentKey
565565
* @param string|null $relatedKey
566566
* @param string|null $relation
567-
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, $this>
567+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, $this, \Illuminate\Database\Eloquent\Relations\Pivot>
568568
*/
569569
public function belongsToMany(
570570
$related,
@@ -624,7 +624,7 @@ public function belongsToMany(
624624
* @param string $parentKey
625625
* @param string $relatedKey
626626
* @param string|null $relationName
627-
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, TDeclaringModel>
627+
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, TDeclaringModel, \Illuminate\Database\Eloquent\Relations\Pivot>
628628
*/
629629
protected function newBelongsToMany(
630630
Builder $query,

src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
/**
2121
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
2222
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
23+
* @template TPivotModel of \Illuminate\Database\Eloquent\Relations\Pivot
2324
*
2425
* @extends \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, TDeclaringModel, \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>>
2526
*/
@@ -128,7 +129,7 @@ class BelongsToMany extends Relation
128129
/**
129130
* The class name of the custom pivot model to use for the relationship.
130131
*
131-
* @var string
132+
* @var class-string<TPivotModel>
132133
*/
133134
protected $using;
134135

@@ -316,7 +317,7 @@ protected function buildDictionary(EloquentCollection $results)
316317
/**
317318
* Get the class being used for pivot models.
318319
*
319-
* @return string
320+
* @return class-string<TPivotModel>
320321
*/
321322
public function getPivotClass()
322323
{
@@ -326,7 +327,7 @@ public function getPivotClass()
326327
/**
327328
* Specify the custom pivot model to use for the relationship.
328329
*
329-
* @param string $class
330+
* @param class-string<TPivotModel> $class
330331
* @return $this
331332
*/
332333
public function using($class)

src/Illuminate/Database/Eloquent/Relations/MorphToMany.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
1212
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
1313
*
14-
* @extends \Illuminate\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, TDeclaringModel>
14+
* @extends \Illuminate\Database\Eloquent\Relations\BelongsToMany<TRelatedModel, TDeclaringModel, \Illuminate\Database\Eloquent\Relations\Pivot>
1515
*/
1616
class MorphToMany extends BelongsToMany
1717
{

types/Database/Eloquent/Relations.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function test(User $user, Post $post, Comment $comment, ChildUser $child): void
4141
assertType('Illuminate\Types\Relations\Post|false', $user->posts()->save(new Post()));
4242
assertType('Illuminate\Types\Relations\Post|false', $user->posts()->saveQuietly(new Post()));
4343

44-
assertType('Illuminate\Database\Eloquent\Relations\BelongsToMany<Illuminate\Types\Relations\Role, Illuminate\Types\Relations\User>', $user->roles());
44+
assertType('Illuminate\Database\Eloquent\Relations\BelongsToMany<Illuminate\Types\Relations\Role, Illuminate\Types\Relations\User, Illuminate\Database\Eloquent\Relations\Pivot>', $user->roles());
4545
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->getResults());
4646
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->find([1]));
4747
assertType('Illuminate\Database\Eloquent\Collection<int, Illuminate\Types\Relations\Role>', $user->roles()->findMany([1, 2, 3]));
@@ -157,11 +157,11 @@ public function latestPost(): HasOne
157157
return $post;
158158
}
159159

160-
/** @return BelongsToMany<Role, $this> */
160+
/** @return BelongsToMany<Role, $this, \Illuminate\Database\Eloquent\Relations\Pivot> */
161161
public function roles(): BelongsToMany
162162
{
163163
$belongsToMany = $this->belongsToMany(Role::class);
164-
assertType('Illuminate\Database\Eloquent\Relations\BelongsToMany<Illuminate\Types\Relations\Role, $this(Illuminate\Types\Relations\User)>', $belongsToMany);
164+
assertType('Illuminate\Database\Eloquent\Relations\BelongsToMany<Illuminate\Types\Relations\Role, $this(Illuminate\Types\Relations\User), Illuminate\Database\Eloquent\Relations\Pivot>', $belongsToMany);
165165

166166
return $belongsToMany;
167167
}

0 commit comments

Comments
 (0)