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 relationships with global scope columns #25368

Merged
merged 1 commit into from
Aug 29, 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
4 changes: 2 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -568,10 +568,10 @@ public function get($columns = ['*'])
// First we'll add the proper select columns onto the query so it is run with
// the proper columns. Then, we will get the results and hydrate out pivot
// models with the result of those columns as a separate model relation.
$columns = $this->query->getQuery()->columns ? [] : $columns;

$builder = $this->query->applyScopes();

$columns = $builder->getQuery()->columns ? [] : $columns;

$models = $builder->addSelect(
$this->shouldSelect($columns)
)->getModels();
Expand Down
6 changes: 4 additions & 2 deletions src/Illuminate/Database/Eloquent/Relations/HasManyThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -461,8 +461,10 @@ public function each(callable $callback, $count = 1000)
*/
protected function prepareQueryBuilder($columns = ['*'])
{
return $this->query->applyScopes()->addSelect(
$this->shouldSelect($this->query->getQuery()->columns ? [] : $columns)
$builder = $this->query->applyScopes();

return $builder->addSelect(
$this->shouldSelect($builder->getQuery()->columns ? [] : $columns)
);
}

Expand Down
35 changes: 35 additions & 0 deletions tests/Integration/Database/EloquentBelongsToManyTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -643,6 +643,20 @@ public function test_custom_related_key()
$post->tagsWithCustomRelatedKey()->updateExistingPivot($tag, ['flag' => 'exclude']);
$this->assertEquals('exclude', $post->tagsWithCustomRelatedKey()->first()->pivot->flag);
}

public function test_global_scope_columns()
{
$tag = Tag::create(['name' => str_random()]);
$post = Post::create(['title' => str_random()]);

DB::table('posts_tags')->insert([
['post_id' => $post->id, 'tag_id' => $tag->id, 'flag' => 'empty'],
]);

$tags = $post->tagsWithGlobalScope;

$this->assertEquals(['id' => 1], $tags[0]->getAttributes());
}
}

class Post extends Model
Expand Down Expand Up @@ -691,6 +705,11 @@ public function tagsWithCustomRelatedKey()
return $this->belongsToMany(Tag::class, 'posts_tags', 'post_id', 'tag_id', 'id', 'name')
->withPivot('flag');
}

public function tagsWithGlobalScope()
{
return $this->belongsToMany(TagWithGlobalScope::class, 'posts_tags', 'post_id', 'tag_id');
}
}

class Tag extends Model
Expand Down Expand Up @@ -735,3 +754,19 @@ class CustomPivot extends Pivot
protected $table = 'posts_tags';
protected $dateFormat = 'U';
}

class TagWithGlobalScope extends Model
{
public $table = 'tags';
public $timestamps = true;
protected $guarded = ['id'];

public static function boot()
{
parent::boot();

static::addGlobalScope(function ($query) {
$query->select('tags.id');
});
}
}
34 changes: 34 additions & 0 deletions tests/Integration/Database/EloquentHasManyThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,19 @@ public function basic_create_and_retrieve()
$this->assertEquals([$mate1->id, $mate2->id], $user->teamMates->pluck('id')->toArray());
$this->assertEquals([$user->id], User::has('teamMates')->pluck('id')->toArray());
}

public function test_global_scope_columns()
{
$user = User::create(['name' => str_random()]);

$team1 = Team::create(['owner_id' => $user->id]);

User::create(['name' => str_random(), 'team_id' => $team1->id]);

$teamMates = $user->teamMatesWithGlobalScope;

$this->assertEquals(['id' => 2, 'owner_id' => 1], $teamMates[0]->getAttributes());
}
}

class User extends Model
Expand All @@ -57,6 +70,27 @@ public function teamMates()
{
return $this->hasManyThrough(self::class, Team::class, 'owner_id', 'team_id');
}

public function teamMatesWithGlobalScope()
{
return $this->hasManyThrough(UserWithGlobalScope::class, Team::class, 'owner_id', 'team_id');
}
}

class UserWithGlobalScope extends Model
{
public $table = 'users';
public $timestamps = false;
protected $guarded = ['id'];

public static function boot()
{
parent::boot();

static::addGlobalScope(function ($query) {
$query->select('users.id');
});
}
}

class Team extends Model
Expand Down