Skip to content

Commit

Permalink
Fix relationships with global scope columns (#25368)
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir authored and taylorotwell committed Aug 29, 2018
1 parent c540162 commit a784efc
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 4 deletions.
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

0 comments on commit a784efc

Please sign in to comment.