laravel icon indicating copy to clipboard operation
laravel copied to clipboard

add new option in relation_name_strategy key to name has many Relations with plural(Field_name)

Open khattab17 opened this issue 3 years ago • 6 comments

add new option in relation_name_strategy key to name has many Relations with plural(Field_name) like : authors , editiors Not (posts_where_author,posts_where_editor ). also in the RelationHelper we should replace _$primaryKey in the studly case to match the [if (Str::snake($relationName) === Str::snake($this->parent->getClassName()))] condtion in the HasMany.php Relation Class

khattab17 avatar Mar 05 '21 12:03 khattab17

Hi @khattab17 - can you add some real world examples of where this is useful? It sounds a bit like it might be better just to rename the foreign key...

coatesap avatar Mar 05 '21 14:03 coatesap

Hi @khattab17 - can you add some real world examples of where this is useful? It sounds a bit like it might be better just to rename the foreign key...

i have a table called posts has two foreign keys ( author_id, editor_id) and both fields linked to users table id field the 'relation_name_strategy' => 'foreign_key' option gives me relations in User models with names posts_where_author,posts_where_editor and i think it should only be authors,editors cause this is the right naming convention in eloquent relations

khattab17 avatar Mar 05 '21 16:03 khattab17

For the schema you described, using the 'foreign_key' naming strategy should give you:

Post::author()
Post::editor()
User::posts_where_author()
User::posts_where_editor()

Having User::authors() or User::editors() wouldn't make any sense as a relation name because the User model would effectively be referring to itself...

coatesap avatar Mar 05 '21 17:03 coatesap

For the schema you described, using the 'foreign_key' naming strategy should give you:

Post::author()
Post::editor()
User::posts_where_author()
User::posts_where_editor()

Having User::authors() or User::editors() wouldn't make any sense as a relation name because the User model would effectively be referring to itself...

sorry it was my fault i fixed it to be

User::editorPosts() User::authorPosts() i think now it make sence also i was having another problem with the 'relation_name_strategy' => 'foreign_key' option , i have another table called comments has foreign key user_id linked with users table its relation was (i set the 'snake_attributes' => false in the config) User::commentsWhereUser() and i think it should be User::comments() cause there is no other foreign key linked with users table in comments table

khattab17 avatar Mar 05 '21 18:03 khattab17

I'm not sure, @coatesap. It think it might be interesting to support this new style. However, I believe it would be better if it came with tests. Moreover, since it has a breaking change, according to semver it cannot go into master nor v1.x; a v2.x branch should be a good candidate. I fully support what @coatesap decides is best for the package.

CristianLlanos avatar Mar 06 '21 06:03 CristianLlanos

I'm conflicted on whether this style of relation naming is a good thing or not. I can see that by removing the word "where", it makes the method/property a bit shorter. However, it does also make the resulting code harder to read. For instance, it would be easy to assume that the following property gave you a list of all staff who were managers, instead of a list of staff managed by the current staff member:

/** @var Employee $joe */
$employees = $joe->managerEmployees;

Whereas the current approach does seem to be a little bit clearer:

$employees = $joe->employeesWhereManager;

coatesap avatar Mar 09 '21 09:03 coatesap