Laravel追加属性不起作用

时间:2018-02-23 10:24:23

标签: laravel laravel-5 laravel-4 eloquent

我的目标是追加每个产品的平均评分,以便我可以在前端显示

我有两个表,一个是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
                                )

所以基本上这应该在我从控制器打电话时将平均评级附加到我的产品上。 但我得到的只是产品表中可用的字段。之前我曾经和我一起工作并且工作得很好但是我不明白为什么这次不工作。 谁能帮帮我吗? 谢谢。

2 个答案:

答案 0 :(得分:3)

我认为对$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)访问它。

答案 1 :(得分:0)

此问题的解决方案是获取此$product->my_rating$product->average_rating之类的数据。正如@tykus在laracast所建议的

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