从FK得到libelle

问题描述:

我可能有一个新手问题。我需要访问链接表的字段“名称”。从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; 

所以,现在,我的愚蠢的问题是怎么写的我的模型允许这样的愚蠢的功能?

谢谢

我的同事回答我的问题,我只是忘了括号添加到我的客户控制器。

控制器:

echo $customer->paysname(); 

型号:

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