2014-02-18 47 views

回答

4
<?php 

class Product extends Eloquent { 

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

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

} 

定義,而不是$appends財產$with屬性。這是一個熱切的負載。

+0

謝謝!你能否發佈你的信息來源? –

+1

它是Laravel 4,https://github.com/laravel/framework/blob/4.2/src/Illuminate/Database/Eloquent/Model.php#L146和https://laravel.com/docs/4.2/eloquent#轉換爲陣列或JSON –

+0

我檢查了這與Laravel 5.3,它的工作原理。這很令人驚訝,這是多麼模糊。我開始認爲這是一個不好的習慣,沒有人跟隨 – Vanlalhriata