@@ -1727,6 +1727,64 @@ public function testHasNested()
1727
1727
$ this ->assertEquals ($ builder ->toSql (), $ result );
1728
1728
}
1729
1729
1730
+ public function testHasNestedWithMorphTo ()
1731
+ {
1732
+ $ model = new EloquentBuilderTestModelParentStub ;
1733
+ $ connection = $ this ->mockConnectionForModel ($ model , '' );
1734
+
1735
+ $ morphToKey = $ model ->morph ()->getMorphType ();
1736
+
1737
+ $ connection ->shouldReceive ('select ' )->once ()->andReturn ([
1738
+ [$ morphToKey => EloquentBuilderTestModelFarRelatedStub::class],
1739
+ [$ morphToKey => EloquentBuilderTestModelOtherFarRelatedStub::class],
1740
+ ]);
1741
+
1742
+ $ builder = $ model ->orWhereHasMorph ('morph ' , [EloquentBuilderTestModelFarRelatedStub::class], function ($ q ) {
1743
+ $ q ->has ('baz ' );
1744
+ })->orWhereHasMorph ('morph ' , [EloquentBuilderTestModelOtherFarRelatedStub::class], function ($ q ) {
1745
+ $ q ->has ('baz ' );
1746
+ });
1747
+
1748
+ $ results = $ model ->has ('morph.baz ' )->toSql ();
1749
+
1750
+ // we need to adjust the expected builder because some parathesis are added,
1751
+ // which doesn't impact the behavior of the test.
1752
+
1753
+ $ builderSql = $ builder ->toSql ();
1754
+ $ builderSql = str_replace (')))) or (( ' , '))) or ( ' , $ builderSql );
1755
+
1756
+ $ this ->assertSame ($ builderSql , $ results );
1757
+ }
1758
+
1759
+ public function testHasNestedWithMorphToAndMultipleSubRelations ()
1760
+ {
1761
+ $ model = new EloquentBuilderTestModelParentStub ;
1762
+ $ connection = $ this ->mockConnectionForModel ($ model , '' );
1763
+
1764
+ $ morphToKey = $ model ->morph ()->getMorphType ();
1765
+
1766
+ $ connection ->shouldReceive ('select ' )->once ()->andReturn ([
1767
+ [$ morphToKey => EloquentBuilderTestModelFarRelatedStub::class],
1768
+ [$ morphToKey => EloquentBuilderTestModelOtherFarRelatedStub::class],
1769
+ ]);
1770
+
1771
+ $ builder = $ model ->orWhereHasMorph ('morph ' , [EloquentBuilderTestModelFarRelatedStub::class], function ($ q ) {
1772
+ $ q ->has ('baz.bam ' );
1773
+ })->orWhereHasMorph ('morph ' , [EloquentBuilderTestModelOtherFarRelatedStub::class], function ($ q ) {
1774
+ $ q ->has ('baz.bam ' );
1775
+ });
1776
+
1777
+ $ results = $ model ->has ('morph.baz.bam ' )->toSql ();
1778
+
1779
+ // we need to adjust the expected builder because some parathesis are added,
1780
+ // which doesn't impact the behavior of the test.
1781
+
1782
+ $ builderSql = $ builder ->toSql ();
1783
+ $ builderSql = str_replace (')))) or (( ' , '))) or ( ' , $ builderSql );
1784
+
1785
+ $ this ->assertSame ($ builderSql , $ results );
1786
+ }
1787
+
1730
1788
public function testOrHasNested ()
1731
1789
{
1732
1790
$ model = new EloquentBuilderTestModelParentStub ;
@@ -2747,6 +2805,8 @@ protected function mockConnectionForModel($model, $database)
2747
2805
$ resolver = m::mock (ConnectionResolverInterface::class, ['connection ' => $ connection ]);
2748
2806
$ class = get_class ($ model );
2749
2807
$ class ::setConnectionResolver ($ resolver );
2808
+
2809
+ return $ connection ;
2750
2810
}
2751
2811
2752
2812
protected function getBuilder ()
@@ -2899,6 +2959,11 @@ public function baz()
2899
2959
{
2900
2960
return $ this ->hasMany (EloquentBuilderTestModelFarRelatedStub::class);
2901
2961
}
2962
+
2963
+ public function bam ()
2964
+ {
2965
+ return $ this ->hasMany (EloquentBuilderTestModelOtherFarRelatedStub::class);
2966
+ }
2902
2967
}
2903
2968
2904
2969
class EloquentBuilderTestModelFarRelatedStub extends Model
@@ -2912,6 +2977,29 @@ public function roles()
2912
2977
'self_id ' ,
2913
2978
);
2914
2979
}
2980
+
2981
+ public function baz ()
2982
+ {
2983
+ return $ this ->belongsTo (EloquentBuilderTestModelCloseRelatedStub::class);
2984
+ }
2985
+ }
2986
+
2987
+ class EloquentBuilderTestModelOtherFarRelatedStub extends Model
2988
+ {
2989
+ public function roles ()
2990
+ {
2991
+ return $ this ->belongsToMany (
2992
+ EloquentBuilderTestModelParentStub::class,
2993
+ 'user_role ' ,
2994
+ 'related_id ' ,
2995
+ 'self_id ' ,
2996
+ );
2997
+ }
2998
+
2999
+ public function baz ()
3000
+ {
3001
+ return $ this ->belongsTo (EloquentBuilderTestModelCloseRelatedStub::class);
3002
+ }
2915
3003
}
2916
3004
2917
3005
class EloquentBuilderTestModelSelfRelatedStub extends Model
0 commit comments