diff --git a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php index 46e7d8c7cc3e..e3257ad2678e 100644 --- a/src/Illuminate/Database/Eloquent/Relations/MorphTo.php +++ b/src/Illuminate/Database/Eloquent/Relations/MorphTo.php @@ -243,6 +243,22 @@ public function getDictionary() return $this->dictionary; } + /** + * Remove all or passed registered global scopes. + * + * @param array|null $scopes + * @return $this + */ + public function withoutGlobalScopes(array $scopes = null) + { + $this->macroBuffer[] = [ + 'method' => __FUNCTION__, + 'parameters' => [$scopes], + ]; + + return $this; + } + /** * Replay stored macro calls on the actual related instance. * diff --git a/tests/Integration/Database/EloquentMorphToGlobalScopesTest.php b/tests/Integration/Database/EloquentMorphToGlobalScopesTest.php new file mode 100644 index 000000000000..c55bee882e45 --- /dev/null +++ b/tests/Integration/Database/EloquentMorphToGlobalScopesTest.php @@ -0,0 +1,83 @@ +increments('id'); + $table->softDeletes(); + }); + + Schema::create('comments', function (Blueprint $table) { + $table->increments('id'); + $table->string('commentable_type'); + $table->integer('commentable_id'); + }); + + $post = Post::create(); + (new Comment)->commentable()->associate($post)->save(); + + $post = tap(Post::create())->delete(); + (new Comment)->commentable()->associate($post)->save(); + } + + public function test_with_global_scopes() + { + $comments = Comment::with('commentable')->get(); + + $this->assertNotNull($comments[0]->commentable); + $this->assertNull($comments[1]->commentable); + } + + public function test_without_global_scope() + { + $comments = Comment::with(['commentable' => function ($query) { + $query->withoutGlobalScopes([SoftDeletingScope::class]); + }])->get(); + + $this->assertNotNull($comments[0]->commentable); + $this->assertNotNull($comments[1]->commentable); + } + + public function test_without_global_scopes() + { + $comments = Comment::with(['commentable' => function ($query) { + $query->withoutGlobalScopes(); + }])->get(); + + $this->assertNotNull($comments[0]->commentable); + $this->assertNotNull($comments[1]->commentable); + } +} + +class Comment extends Model +{ + public $timestamps = false; + + public function commentable() + { + return $this->morphTo(); + } +} + +class Post extends Model +{ + use SoftDeletes; + + public $timestamps = false; +}