1
0
mirror of https://github.com/devfake/flox.git synced 2024-11-16 07:02:31 +01:00
flox/server/app/Category.php
2015-07-27 20:49:37 +02:00

45 lines
940 B
PHP

<?php
namespace Flox;
use Illuminate\Database\Eloquent\Model;
use Cviebrock\EloquentSluggable\SluggableInterface;
use Cviebrock\EloquentSluggable\SluggableTrait;
class Category extends Model implements SluggableInterface
{
use SluggableTrait;
protected $sluggable = [
'build_from' => 'title',
'save_to' => 'slug',
];
public function items()
{
return $this->hasMany('Flox\Item');
}
/**
* http://softonsofa.com/tweaking-eloquent-relations-how-to-get-hasmany-relation-count-efficiently/
*/
public function itemsCount()
{
return $this->hasOne('Flox\Item')
->selectRaw('category_id, count(*) as aggregate')
->groupBy('category_id');
}
public function getItemsCountAttribute()
{
if ( ! $this->relationLoaded('itemsCount')) {
$this->load('itemsCount');
}
$related = $this->getRelation('itemsCount');
return ($related) ? (int) $related->aggregate : 0;
}
}