Laravel 追加属性无效

Laravel append attribute not working

我的objective是附加每个产品的平均评分,这样我就可以在前端显示

我有两个table,一个是products,另一个是reviews

我的review model

class Review extends Model 
{

    protected $table = 'reviews';
    public $timestamps = true;

    use SoftDeletes;

    protected $dates = ['deleted_at'];
    protected $fillable = array('user_id', 'product_id', 'rating', 'feedback');

    public function user()
    {
        return $this->belongsTo('App\Models\User');
    }

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

}

我的product model

protected $appends = ['average_rating','my_rating'];


   // i added these accoceries inside class as per the laravel documentation
 public function reviews()
    {
        return $this->hasMany(Review::class);
    }


    public function getAverageRatingAttribute(){
        return round($this->reviews()->avg('rating'),1);
    }


    public function getMyRatingAttribute(){

        //check if user loged in
        if(Auth::check()){
            return round($this->reviews()->where('user_id',Auth::user()->id)->avg('rating'),1);
        }else{
            return round($this->reviews()->where('user_id',NULL)->avg('rating'),1);
        }
    }

回应

 [original:protected] => Array
                                (
                                    [id] => 1
                                    [user_id] => 1
                                    [sku_code] => 
                                    [name] => Product title
                                    [slug] => product-title
                                    [description] => This is loream ipsum text to check if the  application is working correctly or not
                                    [thumbnail] => images/products/thumbnail/a9fa0b28.jpg
                                    [short_description] => This is loream ipsum to check if update working or not
                                    [featured_image_id] => 
                                    [category_id] => 1
                                    [subcategory_id] => 
                                    [price] => 259.00
                                    [img_height] => 20
                                    [img_width] => 10
                                    [discount_id] => 
                                    [featured_product] => 0
                                    [availability] => 1
                                    [created_at] => 2018-02-22 11:33:27
                                    [updated_at] => 2018-02-22 13:36:21
                                    [deleted_at] => 
                                    [weight] => 100
                                    [weight_unit] => kg
                                )

所以基本上这应该在我从控制器调用时将平均评级附加到我的产品。 但是我得到的只是产品 table 中可用的字段。我以前用过这个,当时工作得很好,但我不明白为什么这次不工作。 谁能帮帮我吗? 谢谢。

此问题的解决方案是获取这样的数据 $product->my_rating$product->average_rating。正如@tykus 在 laracast

所建议的

https://www.laracasts.com/discuss/channels/eloquent/laravel-append-attribute-not-working

我认为对 $appends 的工作原理存在误解。来自 official Laravel docs:

Once the attribute has been added to the appends list, it will be included in both the model's array and JSON forms.

因此如果您打印模型实例本身,它不会出现在模型的 attributes 列表中。但是,附加的属性将出现在 $model->toArray()$model->toJson() 的结果中。它也可以使用 $model->appended_attribute(或在您的情况下为 $model->average_rating)访问。