Skip to content

Commit

Permalink
Fix $withCount
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Apr 26, 2019
1 parent c1910ce commit 7dde63e
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 7 deletions.
44 changes: 37 additions & 7 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,6 @@ public function __construct(Builder $query, Model $parent, array $throughParents
*/
public function addConstraints()
{
$this->query->select([$this->related->getTable().'.*']);

$this->performJoins();

if (static::$constraints) {
Expand Down Expand Up @@ -141,10 +139,6 @@ public function hasSoftDeletes(Model $model)
*/
public function addEagerConstraints(array $models)
{
$this->query->addSelect([
$this->getQualifiedFirstLocalKeyName().' as '.static::THROUGH_KEY,
]);

$keys = $this->getKeys($models, $this->getFirstForeignKeyName());

$this->query->whereIn($this->getQualifiedFirstLocalKeyName(), $keys);
Expand Down Expand Up @@ -215,7 +209,43 @@ protected function buildDictionary(Collection $results)
*/
public function getResults()
{
return $this->query->first();
return $this->first();
}

/**
* Execute the query and get the first result.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Model|object|static|null
*/
public function first($columns = ['*'])
{
if ($columns === ['*']) {
$columns = [$this->related->getTable().'.*'];
}

return $this->query->first($columns);
}

/**
* Execute the query as a "select" statement.
*
* @param array $columns
* @return \Illuminate\Database\Eloquent\Collection
*/
public function get($columns = ['*'])
{
$columns = $this->query->getQuery()->columns ? [] : $columns;

if ($columns === ['*']) {
$columns = [$this->related->getTable().'.*'];
}

$columns[] = $this->getQualifiedFirstLocalKeyName().' as '.static::THROUGH_KEY;

$this->query->addSelect($columns);

return $this->query->get();
}

/**
Expand Down
2 changes: 2 additions & 0 deletions tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ public function testLazyLoading()
$country = Comment::first()->country;

$this->assertEquals(1, $country->id);
$this->assertEquals(1, $country->users_count);
}

public function testLazyLoadingWithSingleThroughModel()
Expand Down Expand Up @@ -49,6 +50,7 @@ public function testEagerLoading()
$this->assertEquals(1, $comments[0]->country->id);
$this->assertEquals(2, $comments[1]->country->id);
$this->assertNull($comments[2]->country);
$this->assertEquals(1, $comments[0]->country->users_count);
}

public function testEagerLoadingWithPrefix()
Expand Down
5 changes: 5 additions & 0 deletions tests/Models/Country.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,10 @@

class Country extends Model
{
protected $withCount = ['users'];

public function users()
{
return $this->hasMany(User::class);
}
}

0 comments on commit 7dde63e

Please sign in to comment.