Laravel 附加属性不起作用

use*_*472 2 laravel eloquent laravel-4 laravel-5

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

我有两张桌子,一张是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');
    }

}
Run Code Online (Sandbox Code Playgroud)

我的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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

回复

 [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
                                )
Run Code Online (Sandbox Code Playgroud)

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

Nav*_*eed 5

我认为对于如何运作存在误解$appends。来自Laravel 官方文档

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根据您的情况)进行访问。