简体   繁体   English

laravel 5.3在通过访问者加载关系后无法访问属性

[英]laravel 5.3 can't access property after loading a relationship via accessor

Initially, i needed to get the average rating from a reviews table for all products. 最初,我需要从所有产品的评论表中获得平均评分。 I've founded the way how to do it, but i am stuck for hours when i want to access the accessors value. 我已经创建了如何做到的方式,但是当我想要访问访问者的价值时,我被困了好几个小时。 I want to display to the view the content of aggregate,but every thing i've tried, dosen't work. 我想在视图中显示聚合的内容,但是我尝试过的每件事都没有用。 Please help. 请帮忙。

Product model 产品型号

protected $table = 'products';

protected $fillable = ['title','body'];



public function scopeActive($query, $default = true){
    $query->where('active',$default)->orderBy('created_at','desc');
}

public function productreviews(){
    return $this->hasMany(Review::class);
}

public function avgRating(){

    return $this->hasOne(Review::class)->selectraw('avg(rating) as aggregate,product_id')->groupBy('product_id');
}


public function getAverageAttribute(){

    if (!$this->relationLoaded('avgRating')){
        $this->load('avgRating');
    }
    $relation = $this->getRelation('avgRating');

     return ($relation) ? $relation->aggregate : null;
}

Review model 审查模型

protected $table = 'reviews';

protected $fillable = ['user_id','body','rating'];

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

ProductsController 的ProductsController

 public function show(Product $product){
   $getAllProducts = $product->with('avgRating')->active()->get();
   return view('products.allproducts',['products'=>$getAllProducts]);

} }

Returned Response from show method 从show方法返回响应

    [{"id":1,"title":"Ipsum quos libero iusto.","body":"Ipsum temporibus              tenetur voluptates.","active":1,"created_at":"2016-11-23   12:44:02","updated_at":"2016-11-23 12:44:02","avg_rating":{"aggregate":4.5,"product_id":1}},{"id":2,"title":"Ab ducimus quia sed quia pariatur officiis.","body":"Cupiditate aut nihil at est.","active":1,"created_at":"2016-11-23 12:44:02","updated_at":"2016-11-23 12:44:02","avg_rating":{"aggregate":4,"product_id":2}}]

View 视图

 @extends('layouts.app')
 @section('content')
 <div class="container">
   <div class="row">
     <div class="col-md-8 col-md-offset-2">
        <div class="panel panel-default">
            <div class="panel-heading">Products</div>

            <div class="panel-body">
                @foreach($products as $product)

                        @if($product->active)
                        <div class='alert alert-info'>
                            <h1>{{ $product->title}}</h1>
                            <p>{{ $product->body }}</p>
                            <p>{{ $product->avg_rating->aggregate }}</p>   
                        </div>
                        @endif


                @endforeach
            </div>
        </div>
      </div>
    </div>
  </div>
@endsection

The solution is writing the function this way: 解决方案是以这种方式编写函数:

public function avgRating() {
    return $this->productreviews()->avg('rating');
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM