向Laravel模型添加属性值的问题

时间:2017-09-09 18:57:04

标签: php laravel oop

我正在尝试在模型中创建一个附加属性方法,该方法将评级标签列附加到现有评级模型。我有完全相同的代码适用于添加点击数据的另一个模型。我得到一个未定义的索引:rating_label错误 - 无法弄清楚为什么?任何帮助非常感谢。

namespace App\Models;

use Illuminate\Database\Eloquent\Model;

class Rating extends Model
{
    protected $table = 'rating';
    protected $primaryKey = 'rating_id';
    protected $appends = array('rating_label');

    /**
     * Define a 1-many inverse relationship in eloquent
     */
    public function Article()
    {

    return $this->belongsTo(Article::class, 'article_id');// (Model, primary_key)
    }
    /**
     * Add additional column to Rating model for use in ClassifyingService class
     */
    public function getRatingLabelAttribute()//accessor getter/setter
    {
        return $this->attributes['rating_label'];
    }   
}

0 个答案:

没有答案