Laravel Eloquent,追加与该关系同名的属性

时间:2014-02-18 10:47:25

标签: laravel laravel-4 eloquent

<?php 

class Product extends Eloquent {

protected appends = array('category');

  public function category()
  {
    return $this->belongsTo('Models\Category',
                            'category_id');
  }

}

如何实现?

1 个答案:

答案 0 :(得分:5)

<?php 

class Product extends Eloquent {

  protected $with = array('category');
  //protected $appends = array('category');

  public function category()
  {
    return $this->belongsTo('Models\Category',
                            'category_id');
  }

}

定义$with属性而不是$appends属性。这是一个急切的负担。