Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.6] Fix MorphTo eager loading and withoutGlobalScopes() #25331

Merged
merged 1 commit into from
Aug 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}