Skip to content

Commit

Permalink
Fix count and existence queries on Laravel 5.0–5.3
Browse files Browse the repository at this point in the history
  • Loading branch information
staudenmeir committed Feb 24, 2019
1 parent 6cf2ba4 commit 2d3c210
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 3 deletions.
3 changes: 2 additions & 1 deletion .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
tools:
external_code_coverage: true
external_code_coverage:
runs: 2
3 changes: 3 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,9 @@ env:

matrix:
include:
- php: 5.6
- php: 5.6
env: COVERAGE=yes RELEASE=lowest
- php: 7.0
- php: 7.1
- php: 7.2
Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
"illuminate/database": "~5.0"
},
"require-dev": {
"phpunit/phpunit": "~6.5"
"phpunit/phpunit": "~5.7|~6.5"
},
"autoload": {
"psr-4": {
Expand Down
25 changes: 25 additions & 0 deletions src/Relations/BelongsToThrough.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,6 +230,31 @@ public function getRelationExistenceQuery(Builder $query, Builder $parent, $colu
return $query;
}

/**
* Add the constraints for a relationship count query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getRelationCountQuery(Builder $query, Builder $parent)
{
return $this->getRelationExistenceQuery($query, $parent, new Expression('count(*)'));
}

/**
* Add the constraints for a relationship query.
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param \Illuminate\Database\Eloquent\Builder $parent
* @param array|mixed $columns
* @return \Illuminate\Database\Eloquent\Builder
*/
public function getRelationQuery(Builder $query, Builder $parent, $columns = ['*'])
{
return $this->getRelationExistenceQuery($query, $parent, $columns);
}

/**
* Get the results of the relationship.
*
Expand Down
3 changes: 2 additions & 1 deletion tests/BelongsToThroughTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Tests;

use Illuminate\Support\Arr;
use Tests\Models\Comment;
use Tests\Models\Post;

Expand Down Expand Up @@ -62,6 +63,6 @@ public function testExistenceQuery()
{
$comments = Comment::has('country')->get();

$this->assertEquals([31, 32, 33], $comments->pluck('id')->all());
$this->assertEquals([31, 32, 33], Arr::pluck($comments, 'id'));
}
}

0 comments on commit 2d3c210

Please sign in to comment.