Search code examples
phplaraveleloquentrelationships

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


I'm writing a web app using the Laravel framework. I have two models where I have created a public function, using the same name, to form an Eloquent Relationship. I want to know if this is bad practice, or if it will cause me any problems.

Here is my code for my WorkOrder model:

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

Here is my code for my Customer model:

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

Solution

  • It's fine as is and will work with no issues, as @Ohgodwhy commented you'd want to pluralize the function name for hasMany or belongsToMany relationships. in this case however it's a moot point as aircraft is the plural of aircraft.