Kent Liau
Kent Liau

Reputation: 925

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 ?

Upvotes: 6

Views: 5612

Answers (1)

Kent Liau
Kent Liau

Reputation: 925

<?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.

Upvotes: 8

Related Questions