2017-10-09 40 views
0

我可能有一個新手問題。我需要訪問鏈接表的字段「名稱」。從FK得到libelle

我有一個country_id鏈接到國家表(簡單)的客戶表。

走進我的客戶模型,我定義了下面的函數

public function country() { 
    return $this->belongsTo(Country::class, 'country_id'); 
} 

所以,在我的CustomerController,我訪問了國名用下面的代碼。

echo ($customer->country_id != null ? $customer->country->name : ''); 

但是,我想什麼來,就是要改變客戶控制器這樣的:

echo $customer->country_name; 

所以,現在,我的愚蠢的問題是怎麼寫的我的模型允許這樣的愚蠢的功能?

謝謝

回答

0

我的同事回答我的問題,我只是忘了括號添加到我的客戶控制器。

控制器:

echo $customer->paysname(); 

型號:

public function paysname() { 
    return (is_null($this->pays_id) ? '' : $this->pays->name); 
}