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

Ken*_*iau 4 laravel eloquent laravel-4

<?php 

class Product extends Eloquent {

protected appends = array('category');

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

}
Run Code Online (Sandbox Code Playgroud)

如何实现呢?

Ken*_*iau 5

<?php 

class Product extends Eloquent {

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

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

}
Run Code Online (Sandbox Code Playgroud)

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