Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel Eloquent, appends attribute that is same name with the relationship

<?php 

class Product extends Eloquent {

protected appends = array('category');

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

}

How to achieve that ?

like image 304
Kent Liau Avatar asked Feb 18 '14 10:02

Kent Liau


1 Answers

<?php 

class Product extends Eloquent {

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

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

}

Define a $with property instead of $appends property. It is an eager load.

like image 171
Kent Liau Avatar answered Sep 28 '22 01:09

Kent Liau