Skip to content

Commit

Permalink
Added countWhereHas() query method
Browse files Browse the repository at this point in the history
  • Loading branch information
micheleangioni committed Mar 28, 2015
1 parent c6a6759 commit 0b76a03
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ The `AbstractEloquentRepository` empowers automatically our repositories of the
- truncate()
- count()
- countBy(array $where = array())
- countWhereHas($relation, array $where = array(), array $whereHas = array())

The Repos module also supports xml repositories. Suppose we have a staff.xml file. We need to define a `StaffXMLRepositoryInterface`

Expand Down
29 changes: 29 additions & 0 deletions src/MicheleAngioni/Support/Repos/AbstractEloquentRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,35 @@ public function countBy(array $where = array())
return $query->count();
}

/**
* Count all results that have a required relationship with input constraints
*
* @param string $relation
* @param array $where
* @param array $whereHas
*
* @return int
*/
public function countWhereHas($relation, array $where = array(), array $whereHas = array())
{
$query = $this->make();

foreach($where as $key => $value)
{
$query = $query->where($key, '=', $value);
}

$query = $query->whereHas($relation, function($q) use($whereHas)
{
foreach($whereHas as $key => $value)
{
$q = $q->where($key, '=', $value);
}
});

return $query->count();
}


// <--- INTERNALLY USED METHODS --->

Expand Down

0 comments on commit 0b76a03

Please sign in to comment.