@@ -544,32 +544,35 @@ public function relationCount(Query $query, array $relations, string $aggregate
544
544
*
545
545
* @param string $model 模型名
546
546
* @param string $foreignKey 关联外键
547
+ * @param string $localKey 当前主键
547
548
*
548
549
* @return HasOne
549
550
*/
550
- public function hasOne (string $ model , string $ foreignKey = '' ): HasOne
551
+ public function hasOne (string $ model , string $ foreignKey = '' , string $ localKey = '' ): HasOne
551
552
{
552
553
// 记录当前关联信息
553
554
$ model = $ this ->parseRelationModel ($ model );
555
+ $ localKey = $ localKey ?: $ this ->getPk ();
554
556
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ($ this ->getName ());
555
557
556
- return new HasOne ($ this , $ model , $ foreignKey , $ this -> getPk () );
558
+ return new HasOne ($ this , $ model , $ foreignKey , $ localKey );
557
559
}
558
560
559
561
/**
560
562
* BELONGS TO 关联定义.
561
563
*
562
564
* @param string $model 模型名
563
565
* @param string $foreignKey 关联外键
566
+ * @param string $localKey 关联主键
564
567
*
565
568
* @return BelongsTo
566
569
*/
567
- public function belongsTo (string $ model , string $ foreignKey = '' ): BelongsTo
570
+ public function belongsTo (string $ model , string $ foreignKey = '' , string $ localKey = '' ): BelongsTo
568
571
{
569
572
// 记录当前关联信息
570
573
$ model = $ this ->parseRelationModel ($ model );
571
574
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ((new $ model ())->getName ());
572
- $ localKey = (new $ model ())->getPk ();
575
+ $ localKey = $ localKey ?: (new $ model ())->getPk ();
573
576
$ trace = debug_backtrace (DEBUG_BACKTRACE_IGNORE_ARGS , 2 );
574
577
$ relation = Str::snake ($ trace [1 ]['function ' ]);
575
578
@@ -581,16 +584,18 @@ public function belongsTo(string $model, string $foreignKey = ''): BelongsTo
581
584
*
582
585
* @param string $model 模型名
583
586
* @param string $foreignKey 关联外键
587
+ * @param string $localKey 当前主键
584
588
*
585
589
* @return HasMany
586
590
*/
587
- public function hasMany (string $ model , string $ foreignKey = '' ): HasMany
591
+ public function hasMany (string $ model , string $ foreignKey = '' , string $ localKey = '' ): HasMany
588
592
{
589
593
// 记录当前关联信息
590
594
$ model = $ this ->parseRelationModel ($ model );
595
+ $ localKey = $ localKey ?: $ this ->getPk ();
591
596
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ($ this ->getName ());
592
597
593
- return new HasMany ($ this , $ model , $ foreignKey , $ this -> getPk () );
598
+ return new HasMany ($ this , $ model , $ foreignKey , $ localKey );
594
599
}
595
600
596
601
/**
@@ -600,6 +605,8 @@ public function hasMany(string $model, string $foreignKey = ''): HasMany
600
605
* @param string $through 中间模型名
601
606
* @param string $foreignKey 关联外键
602
607
* @param string $throughKey 关联外键
608
+ * @param string $localKey 当前主键
609
+ * @param string $throughPk 中间表主键
603
610
*
604
611
* @return HasManyThrough
605
612
*/
@@ -608,10 +615,10 @@ public function hasManyThrough(string $model, string $through, string $foreignKe
608
615
// 记录当前关联信息
609
616
$ model = $ this ->parseRelationModel ($ model );
610
617
$ through = $ this ->parseRelationModel ($ through );
618
+ $ localKey = $ localKey ?: $ this ->getPk ();
611
619
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ($ this ->getName ());
612
620
$ throughKey = $ throughKey ?: $ this ->getForeignKey ((new $ through ())->getName ());
613
- $ localKey = $ this ->getPk ();
614
- $ throughPk = (new $ through ())->getPk ();
621
+ $ throughPk = $ throughPk ?: (new $ through ())->getPk ();
615
622
616
623
return new HasManyThrough ($ this , $ model , $ through , $ foreignKey , $ throughKey , $ localKey , $ throughPk );
617
624
}
@@ -623,18 +630,20 @@ public function hasManyThrough(string $model, string $through, string $foreignKe
623
630
* @param string $through 中间模型名
624
631
* @param string $foreignKey 关联外键
625
632
* @param string $throughKey 关联外键
633
+ * @param string $localKey 当前主键
634
+ * @param string $throughPk 中间表主键
626
635
*
627
636
* @return HasOneThrough
628
637
*/
629
- public function hasOneThrough (string $ model , string $ through , string $ foreignKey = '' , string $ throughKey = '' ): HasOneThrough
638
+ public function hasOneThrough (string $ model , string $ through , string $ foreignKey = '' , string $ throughKey = '' , string $ localKey = '' , string $ throughPk = '' ): HasOneThrough
630
639
{
631
640
// 记录当前关联信息
632
641
$ model = $ this ->parseRelationModel ($ model );
633
642
$ through = $ this ->parseRelationModel ($ through );
643
+ $ localKey = $ localKey ?: $ this ->getPk ();
634
644
$ foreignKey = $ foreignKey ?: $ this ->getForeignKey ($ this ->getName ());
635
645
$ throughKey = $ throughKey ?: $ this ->getForeignKey ((new $ through ())->getName ());
636
- $ localKey = $ this ->getPk ();
637
- $ throughPk = (new $ through ())->getPk ();
646
+ $ throughPk = $ throughPk ?: (new $ through ())->getPk ();
638
647
639
648
return new HasOneThrough ($ this , $ model , $ through , $ foreignKey , $ throughKey , $ localKey , $ throughPk );
640
649
}
0 commit comments