使用雄辩的关系统计产品和品牌评论

时间:2014-11-19 18:52:57

标签: php laravel laravel-4 eloquent

我有三种模式:

class Brand extends \Eloquent {
protected $fillable = [];

public function product()
{
    return $this->hasMany('Product');
}
}



class Product extends \Eloquent {

protected $fillable = [];

public function reviews()
{
    return $this->hasMany('Review');
}   

public function brand()
{
    return $this->belongsTo('Brand');
}

}

class Review extends \Eloquent {
protected $fillable = [];

public function product()
{
    return $this->belongsTo('Product');
}   

}

我试图在视图中显示品牌名称以及产品和评论计数:

{{ $brand->product->count() }} 

它适用于此,但不显示评论计数:

{{ $brand->product->reviews->count() }}

既不是:

{{ $brand->product->reviews->count() }}

我得到的错误是:

ErrorException (E_UNKNOWN) 
Undefined property: Illuminate\Database\Eloquent\Collection::$review

ErrorException (E_UNKNOWN) 
Undefined property: Illuminate\Database\Eloquent\Collection::$reviews

2 个答案:

答案 0 :(得分:1)

问题在于,您无法在模型集合上调用关系,而只能在模型本身上调用关系。这意味着您必须遍历产品并计算每个产品的评论。

基本上就是那样

$counter = 0;
foreach($brand->product as $product){
    $counter += $product->reviews()->count();
}
echo $counter.' reviews!';

现在这对数据库性能非常糟糕。首先,它查询产品,并为每个产品向数据库发出另一个请求。我们可以使用eager loading来避免这种情况。

$counter = 0;
$products = $brand->product()->with('reviews')->get();
foreach($products as $product){
    $counter += $product->reviews()->count();
}
echo $counter.' reviews!';

通过热切加载,当我们执行$product->reviews()

时,它会使用一个查询加载所有数据并将其加载到内存中

为了完成这里的工作,我们现在可以将其置于品牌模型中的功能

public function getProductReviewCount(){
    $counter = 0;
    $products = $this->product()->with('reviews')->get();
    foreach($products as $product){
        $counter += $product->reviews()->count();
    }
    return $counter;
}

{{ $brand->getProductReviewCount() }}

旁注:我还建议您将关系product的名称更改为products。它更有意义,通常使用复数形式。

答案 1 :(得分:0)

我正在使用         {{count($ brand-> product)}}         {{count($ brand-> product-> reviews)}}