Skip to content

Commit

Permalink
Fix MorphTo eager loading and withoutGlobalScopes()
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Aug 25, 2018
1 parent 4c0f7ab commit 6beb74b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/Illuminate/Database/Eloquent/Relations/MorphTo.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
83 changes: 83 additions & 0 deletions tests/Integration/Database/EloquentMorphToGlobalScopesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

namespace Illuminate\Tests\Integration\Database\EloquentMorphToGlobalScopesTest;

use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Tests\Integration\Database\DatabaseTestCase;

/**
* @group integration
*/
class EloquentMorphToGlobalScopesTest extends DatabaseTestCase
{
public function setUp()
{
parent::setUp();

Schema::create('posts', function (Blueprint $table) {
$table->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;
}

0 comments on commit 6beb74b

Please sign in to comment.