2016-01-29 23 views

回答

0

,您可以查看如何做到這一點,其他很多人,通過閱讀documents by Laravel。他們是一個很好的幫助,這個特定的問題有一個例子和一切。話雖如此,我會幫助你開始。

  • 定義在任一模式,informationinformation_description,或兩者的關係。爲簡潔起見,我將僅使用information
  • 通過foreign_keylocal_key關聯hasMany()關係,因爲它不同於Laravel的默認行爲。

因此,我們有一個模型,現在看起來像:

class Information extends Model 
{ 
    /** 
    * Get the descriptions for the Information model. 
    * Note the 2nd and 3rd arguments in the method 
    * which define foreign_key and local_key. 
    */ 
    public function description() 
    { 
     return $this->hasMany('App\InformationDescription', 'information_id', 'information_id'); 
    } 
} 

現在您已經定義的關係,你可以進行查詢。

// Get the description for the information 
$information = App\Information::find(1)->description; 

// Iterate over the results 
foreach ($information as $description) { 
    $description->title; 
} 

使用的表命名約定有點奇怪,但如果我理解正確,這將工作。希望能幫助到你。