20
20
/**
21
21
* @template TRelatedModel of \Illuminate\Database\Eloquent\Model
22
22
* @template TDeclaringModel of \Illuminate\Database\Eloquent\Model
23
- * @template TPivotModel of \Illuminate\Database\Eloquent\Relations\Pivot
23
+ * @template TPivotModel of \Illuminate\Database\Eloquent\Relations\Pivot = \Illuminate\Database\Eloquent\Relations\Pivot
24
+ * @template TAccessor of string = 'pivot'
24
25
*
25
- * @extends \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, TDeclaringModel, \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>>
26
+ * @extends \Illuminate\Database\Eloquent\Relations\Relation<TRelatedModel, TDeclaringModel, \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>>
27
+ *
28
+ * @todo use TAccessor when PHPStan bug is fixed: https://github.com/phpstan/phpstan/issues/12756
26
29
*/
27
30
class BelongsToMany extends Relation
28
31
{
@@ -136,7 +139,7 @@ class BelongsToMany extends Relation
136
139
/**
137
140
* The name of the accessor to use for the "pivot" relationship.
138
141
*
139
- * @var string
142
+ * @var TAccessor
140
143
*/
141
144
protected $ accessor = 'pivot ' ;
142
145
@@ -327,8 +330,12 @@ public function getPivotClass()
327
330
/**
328
331
* Specify the custom pivot model to use for the relationship.
329
332
*
330
- * @param class-string<TPivotModel> $class
333
+ * @template TNewPivotModel of \Illuminate\Database\Eloquent\Relations\Pivot
334
+ *
335
+ * @param class-string<TNewPivotModel> $class
331
336
* @return $this
337
+ *
338
+ * @phpstan-this-out static<TRelatedModel, TDeclaringModel, TNewPivotModel, TAccessor>
332
339
*/
333
340
public function using ($ class )
334
341
{
@@ -340,8 +347,12 @@ public function using($class)
340
347
/**
341
348
* Specify the custom pivot accessor to use for the relationship.
342
349
*
343
- * @param string $accessor
350
+ * @template TNewAccessor of string
351
+ *
352
+ * @param TNewAccessor $accessor
344
353
* @return $this
354
+ *
355
+ * @phpstan-this-out static<TRelatedModel, TDeclaringModel, TPivotModel, TNewAccessor>
345
356
*/
346
357
public function as ($ accessor )
347
358
{
@@ -580,7 +591,11 @@ public function orderByPivot($column, $direction = 'asc')
580
591
*
581
592
* @param mixed $id
582
593
* @param array $columns
583
- * @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel)
594
+ * @return (
595
+ * $id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
596
+ * ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>
597
+ * : TRelatedModel&object{pivot: TPivotModel}
598
+ * )
584
599
*/
585
600
public function findOrNew ($ id , $ columns = ['* ' ])
586
601
{
@@ -596,7 +611,7 @@ public function findOrNew($id, $columns = ['*'])
596
611
*
597
612
* @param array $attributes
598
613
* @param array $values
599
- * @return TRelatedModel
614
+ * @return TRelatedModel&object{pivot: TPivotModel}
600
615
*/
601
616
public function firstOrNew (array $ attributes = [], array $ values = [])
602
617
{
@@ -614,7 +629,7 @@ public function firstOrNew(array $attributes = [], array $values = [])
614
629
* @param array $values
615
630
* @param array $joining
616
631
* @param bool $touch
617
- * @return TRelatedModel
632
+ * @return TRelatedModel&object{pivot: TPivotModel}
618
633
*/
619
634
public function firstOrCreate (array $ attributes = [], array $ values = [], array $ joining = [], $ touch = true )
620
635
{
@@ -640,7 +655,7 @@ public function firstOrCreate(array $attributes = [], array $values = [], array
640
655
* @param array $values
641
656
* @param array $joining
642
657
* @param bool $touch
643
- * @return TRelatedModel
658
+ * @return TRelatedModel&object{pivot: TPivotModel}
644
659
*/
645
660
public function createOrFirst (array $ attributes = [], array $ values = [], array $ joining = [], $ touch = true )
646
661
{
@@ -666,7 +681,7 @@ public function createOrFirst(array $attributes = [], array $values = [], array
666
681
* @param array $values
667
682
* @param array $joining
668
683
* @param bool $touch
669
- * @return TRelatedModel
684
+ * @return TRelatedModel&object{pivot: TPivotModel}
670
685
*/
671
686
public function updateOrCreate (array $ attributes , array $ values = [], array $ joining = [], $ touch = true )
672
687
{
@@ -684,7 +699,11 @@ public function updateOrCreate(array $attributes, array $values = [], array $joi
684
699
*
685
700
* @param mixed $id
686
701
* @param array $columns
687
- * @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel|null)
702
+ * @return (
703
+ * $id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
704
+ * ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>
705
+ * : (TRelatedModel&object{pivot: TPivotModel})|null
706
+ * )
688
707
*/
689
708
public function find ($ id , $ columns = ['* ' ])
690
709
{
@@ -702,7 +721,7 @@ public function find($id, $columns = ['*'])
702
721
*
703
722
* @param mixed $id
704
723
* @param array $columns
705
- * @return TRelatedModel
724
+ * @return TRelatedModel&object{pivot: TPivotModel}
706
725
*
707
726
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
708
727
* @throws \Illuminate\Database\MultipleRecordsFoundException
@@ -719,7 +738,7 @@ public function findSole($id, $columns = ['*'])
719
738
*
720
739
* @param \Illuminate\Contracts\Support\Arrayable|array $ids
721
740
* @param array $columns
722
- * @return \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>
741
+ * @return \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel} >
723
742
*/
724
743
public function findMany ($ ids , $ columns = ['* ' ])
725
744
{
@@ -739,7 +758,11 @@ public function findMany($ids, $columns = ['*'])
739
758
*
740
759
* @param mixed $id
741
760
* @param array $columns
742
- * @return ($id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>) ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel> : TRelatedModel)
761
+ * @return (
762
+ * $id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
763
+ * ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel}>
764
+ * : TRelatedModel&object{pivot: TPivotModel}
765
+ * )
743
766
*
744
767
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
745
768
*/
@@ -770,8 +793,8 @@ public function findOrFail($id, $columns = ['*'])
770
793
* @param (\Closure(): TValue)|null $callback
771
794
* @return (
772
795
* $id is (\Illuminate\Contracts\Support\Arrayable<array-key, mixed>|array<mixed>)
773
- * ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel>|TValue
774
- * : TRelatedModel|TValue
796
+ * ? \Illuminate\Database\Eloquent\Collection<int, TRelatedModel&object{pivot: TPivotModel} >|TValue
797
+ * : ( TRelatedModel&object{pivot: TPivotModel}) |TValue
775
798
* )
776
799
*/
777
800
public function findOr ($ id , $ columns = ['* ' ], ?Closure $ callback = null )
@@ -804,7 +827,7 @@ public function findOr($id, $columns = ['*'], ?Closure $callback = null)
804
827
* @param mixed $operator
805
828
* @param mixed $value
806
829
* @param string $boolean
807
- * @return TRelatedModel|null
830
+ * @return ( TRelatedModel&object{pivot: TPivotModel}) |null
808
831
*/
809
832
public function firstWhere ($ column , $ operator = null , $ value = null , $ boolean = 'and ' )
810
833
{
@@ -815,7 +838,7 @@ public function firstWhere($column, $operator = null, $value = null, $boolean =
815
838
* Execute the query and get the first result.
816
839
*
817
840
* @param array $columns
818
- * @return TRelatedModel|null
841
+ * @return ( TRelatedModel&object{pivot: TPivotModel}) |null
819
842
*/
820
843
public function first ($ columns = ['* ' ])
821
844
{
@@ -828,7 +851,7 @@ public function first($columns = ['*'])
828
851
* Execute the query and get the first result or throw an exception.
829
852
*
830
853
* @param array $columns
831
- * @return TRelatedModel
854
+ * @return TRelatedModel&object{pivot: TPivotModel}
832
855
*
833
856
* @throws \Illuminate\Database\Eloquent\ModelNotFoundException<TRelatedModel>
834
857
*/
@@ -848,7 +871,7 @@ public function firstOrFail($columns = ['*'])
848
871
*
849
872
* @param (\Closure(): TValue)|list<string> $columns
850
873
* @param (\Closure(): TValue)|null $callback
851
- * @return TRelatedModel|TValue
874
+ * @return ( TRelatedModel&object{pivot: TPivotModel}) |TValue
852
875
*/
853
876
public function firstOr ($ columns = ['* ' ], ?Closure $ callback = null )
854
877
{
@@ -942,7 +965,7 @@ protected function aliasedPivotColumns()
942
965
* @param array $columns
943
966
* @param string $pageName
944
967
* @param int|null $page
945
- * @return \Illuminate\Pagination\LengthAwarePaginator
968
+ * @return \Illuminate\Pagination\LengthAwarePaginator<int, TRelatedModel&object{pivot: TPivotModel}>
946
969
*/
947
970
public function paginate ($ perPage = null , $ columns = ['* ' ], $ pageName = 'page ' , $ page = null )
948
971
{
@@ -960,7 +983,7 @@ public function paginate($perPage = null, $columns = ['*'], $pageName = 'page',
960
983
* @param array $columns
961
984
* @param string $pageName
962
985
* @param int|null $page
963
- * @return \Illuminate\Contracts\Pagination\Paginator
986
+ * @return \Illuminate\Contracts\Pagination\Paginator<int, TRelatedModel&object{pivot: TPivotModel}>
964
987
*/
965
988
public function simplePaginate ($ perPage = null , $ columns = ['* ' ], $ pageName = 'page ' , $ page = null )
966
989
{
@@ -978,7 +1001,7 @@ public function simplePaginate($perPage = null, $columns = ['*'], $pageName = 'p
978
1001
* @param array $columns
979
1002
* @param string $cursorName
980
1003
* @param string|null $cursor
981
- * @return \Illuminate\Contracts\Pagination\CursorPaginator
1004
+ * @return \Illuminate\Contracts\Pagination\CursorPaginator<int, TRelatedModel&object{pivot: TPivotModel}>
982
1005
*/
983
1006
public function cursorPaginate ($ perPage = null , $ columns = ['* ' ], $ cursorName = 'cursor ' , $ cursor = null )
984
1007
{
@@ -1100,7 +1123,7 @@ public function each(callable $callback, $count = 1000)
1100
1123
* Query lazily, by chunks of the given size.
1101
1124
*
1102
1125
* @param int $chunkSize
1103
- * @return \Illuminate\Support\LazyCollection<int, TRelatedModel>
1126
+ * @return \Illuminate\Support\LazyCollection<int, TRelatedModel&object{pivot: TPivotModel} >
1104
1127
*/
1105
1128
public function lazy ($ chunkSize = 1000 )
1106
1129
{
@@ -1117,7 +1140,7 @@ public function lazy($chunkSize = 1000)
1117
1140
* @param int $chunkSize
1118
1141
* @param string|null $column
1119
1142
* @param string|null $alias
1120
- * @return \Illuminate\Support\LazyCollection<int, TRelatedModel>
1143
+ * @return \Illuminate\Support\LazyCollection<int, TRelatedModel&object{pivot: TPivotModel} >
1121
1144
*/
1122
1145
public function lazyById ($ chunkSize = 1000 , $ column = null , $ alias = null )
1123
1146
{
@@ -1140,7 +1163,7 @@ public function lazyById($chunkSize = 1000, $column = null, $alias = null)
1140
1163
* @param int $chunkSize
1141
1164
* @param string|null $column
1142
1165
* @param string|null $alias
1143
- * @return \Illuminate\Support\LazyCollection<int, TRelatedModel>
1166
+ * @return \Illuminate\Support\LazyCollection<int, TRelatedModel&object{pivot: TPivotModel} >
1144
1167
*/
1145
1168
public function lazyByIdDesc ($ chunkSize = 1000 , $ column = null , $ alias = null )
1146
1169
{
@@ -1160,7 +1183,7 @@ public function lazyByIdDesc($chunkSize = 1000, $column = null, $alias = null)
1160
1183
/**
1161
1184
* Get a lazy collection for the given query.
1162
1185
*
1163
- * @return \Illuminate\Support\LazyCollection<int, TRelatedModel>
1186
+ * @return \Illuminate\Support\LazyCollection<int, TRelatedModel&object{pivot: TPivotModel} >
1164
1187
*/
1165
1188
public function cursor ()
1166
1189
{
@@ -1300,7 +1323,7 @@ public function allRelatedIds()
1300
1323
* @param TRelatedModel $model
1301
1324
* @param array $pivotAttributes
1302
1325
* @param bool $touch
1303
- * @return TRelatedModel
1326
+ * @return TRelatedModel&object{pivot: TPivotModel}
1304
1327
*/
1305
1328
public function save (Model $ model , array $ pivotAttributes = [], $ touch = true )
1306
1329
{
@@ -1317,7 +1340,7 @@ public function save(Model $model, array $pivotAttributes = [], $touch = true)
1317
1340
* @param TRelatedModel $model
1318
1341
* @param array $pivotAttributes
1319
1342
* @param bool $touch
1320
- * @return TRelatedModel
1343
+ * @return TRelatedModel&object{pivot: TPivotModel}
1321
1344
*/
1322
1345
public function saveQuietly (Model $ model , array $ pivotAttributes = [], $ touch = true )
1323
1346
{
@@ -1368,7 +1391,7 @@ public function saveManyQuietly($models, array $pivotAttributes = [])
1368
1391
* @param array $attributes
1369
1392
* @param array $joining
1370
1393
* @param bool $touch
1371
- * @return TRelatedModel
1394
+ * @return TRelatedModel&object{pivot: TPivotModel}
1372
1395
*/
1373
1396
public function create (array $ attributes = [], array $ joining = [], $ touch = true )
1374
1397
{
@@ -1391,7 +1414,7 @@ public function create(array $attributes = [], array $joining = [], $touch = tru
1391
1414
*
1392
1415
* @param iterable $records
1393
1416
* @param array $joinings
1394
- * @return array<int, TRelatedModel>
1417
+ * @return array<int, TRelatedModel&object{pivot: TPivotModel} >
1395
1418
*/
1396
1419
public function createMany (iterable $ records , array $ joinings = [])
1397
1420
{
@@ -1625,7 +1648,7 @@ public function getRelationName()
1625
1648
/**
1626
1649
* Get the name of the pivot accessor for this relationship.
1627
1650
*
1628
- * @return string
1651
+ * @return TAccessor
1629
1652
*/
1630
1653
public function getPivotAccessor ()
1631
1654
{
0 commit comments