有两个 public 函数,在不同的模型中,但名称相同,有什么问题吗?

Any issue with having two public functions, in different models, but with the same name?

我正在使用 Laravel 框架编写网络应用程序。我有两个模型,其中我创建了一个 public 函数,使用相同的名称,以形成 Eloquent 关系。我想知道这是否是不好的做法,或者它是否会给我带来任何问题。

这是我的 WorkOrder 模型的代码:

/**
 * Get the aircraft that owns the work order.
 */
public function aircraft()
{
    return $this->belongsTo(Aircraft::class);
}

这是我的客户模型代码:

/**
 * Get all of the aircraft for the customer.
 */
public function aircraft()
{
    return $this->hasMany(Aircraft::class);
}

它很好,并且可以正常工作,正如@Ohgodwhy 评论的那样,您希望将 hasManybelongsToMany 关系的函数名称复数化。然而,在这种情况下,这是一个有争议的问题,因为飞机是飞机的复数形式。