for testing and deploying your application
for finding and fixing issues
for empowering human code reviews
<?php
/*
* This file is part of the Tinyissue package.
*
* (c) Mohamed Alsharaf <[email protected]>
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Tinyissue\Model\Traits;
/**
* CountAttributeTrait is trait class for adding method to return count attribute.
* @author Mohamed Alsharaf <[email protected]>
* @property array $relations
* @method \Illuminate\Database\Eloquent\Model load($relations)
* @method \Illuminate\Database\Eloquent\Model getRelation($relation)
trait CountAttributeTrait
{
* Returns the aggregate value of a field.
* @param string $field
* @return int
protected function getCountAttribute($field)
// if relation is not loaded already, let's do it first
if (!array_key_exists($field, $this->relations)) {
$this->load($field);
}
$related = $this->getRelation($field);
// then return the count directly
return ($related) ? (int) $related->aggregate : 0;