4
4
5
5
use Illuminate \Database \Eloquent \Model ;
6
6
use Illuminate \Database \Eloquent \Relations \MorphTo ;
7
+ use Illuminate \Database \Eloquent \SoftDeletes ;
7
8
use Illuminate \Database \Schema \Blueprint ;
8
9
use Illuminate \Support \Facades \Schema ;
9
10
use Illuminate \Tests \Integration \Database \DatabaseTestCase ;
@@ -14,6 +15,7 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
14
15
{
15
16
Schema::create ('users ' , function (Blueprint $ table ) {
16
17
$ table ->increments ('id ' );
18
+ $ table ->softDeletes ();
17
19
});
18
20
19
21
Schema::create ('posts ' , function (Blueprint $ table ) {
@@ -25,20 +27,30 @@ protected function defineDatabaseMigrationsAfterDatabaseRefreshed()
25
27
$ table ->increments ('video_id ' );
26
28
});
27
29
30
+ Schema::create ('actions ' , function (Blueprint $ table ) {
31
+ $ table ->increments ('id ' );
32
+ $ table ->string ('target_type ' );
33
+ $ table ->integer ('target_id ' );
34
+ });
35
+
28
36
Schema::create ('comments ' , function (Blueprint $ table ) {
29
37
$ table ->increments ('id ' );
30
38
$ table ->string ('commentable_type ' );
31
39
$ table ->integer ('commentable_id ' );
32
40
});
33
41
34
42
$ user = User::create ();
43
+ $ user2 = User::forceCreate (['deleted_at ' => now ()]);
35
44
36
45
$ post = tap ((new Post )->user ()->associate ($ user ))->save ();
37
46
38
47
$ video = Video::create ();
39
48
40
49
(new Comment )->commentable ()->associate ($ post )->save ();
41
50
(new Comment )->commentable ()->associate ($ video )->save ();
51
+
52
+ (new Action )->target ()->associate ($ video )->save ();
53
+ (new Action )->target ()->associate ($ user2 )->save ();
42
54
}
43
55
44
56
public function testWithMorphLoading ()
@@ -49,9 +61,13 @@ public function testWithMorphLoading()
49
61
}])
50
62
->get ();
51
63
64
+ $ this ->assertCount (2 , $ comments );
65
+
52
66
$ this ->assertTrue ($ comments [0 ]->relationLoaded ('commentable ' ));
67
+ $ this ->assertInstanceOf (Post::class, $ comments [0 ]->getRelation ('commentable ' ));
53
68
$ this ->assertTrue ($ comments [0 ]->commentable ->relationLoaded ('user ' ));
54
69
$ this ->assertTrue ($ comments [1 ]->relationLoaded ('commentable ' ));
70
+ $ this ->assertInstanceOf (Video::class, $ comments [1 ]->getRelation ('commentable ' ));
55
71
}
56
72
57
73
public function testWithMorphLoadingWithSingleRelation ()
@@ -65,6 +81,30 @@ public function testWithMorphLoadingWithSingleRelation()
65
81
$ this ->assertTrue ($ comments [0 ]->relationLoaded ('commentable ' ));
66
82
$ this ->assertTrue ($ comments [0 ]->commentable ->relationLoaded ('user ' ));
67
83
}
84
+
85
+ public function testMorphLoadingMixedWithTrashedRelations ()
86
+ {
87
+ $ action = Action::query ()
88
+ ->with ('target ' )
89
+ ->get ();
90
+
91
+ $ this ->assertCount (2 , $ action );
92
+
93
+ $ this ->assertTrue ($ action [0 ]->relationLoaded ('target ' ));
94
+ $ this ->assertInstanceOf (Video::class, $ action [0 ]->getRelation ('target ' ));
95
+ $ this ->assertTrue ($ action [1 ]->relationLoaded ('target ' ));
96
+ $ this ->assertInstanceOf (User::class, $ action [1 ]->getRelation ('target ' ));
97
+ }
98
+ }
99
+
100
+ class Action extends Model
101
+ {
102
+ public $ timestamps = false ;
103
+
104
+ public function target ()
105
+ {
106
+ return $ this ->morphTo ()->withTrashed ();
107
+ }
68
108
}
69
109
70
110
class Comment extends Model
@@ -90,6 +130,8 @@ public function user()
90
130
91
131
class User extends Model
92
132
{
133
+ use SoftDeletes;
134
+
93
135
public $ timestamps = false ;
94
136
}
95
137
0 commit comments