Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature Proposal - Improved implementation of the use of the joinRelationship function #190

Open
walter-developer opened this issue Nov 2, 2024 · 0 comments

Comments

@walter-developer
Copy link

walter-developer commented Nov 2, 2024

1 - Prefix the name of the table that is inside the connection model (JOIN) together with the where conditions, when the table and column are not referenced together.

Também seria legal as junçõe join, terem por padrão um alias ( AS ) com o nome do metodo de jução no model, ao ivéns de apenas o nome da tabela

My Current Query Example:

User::joinRelationship('posts', fn ($join) => $join->where(' my_schema.posts.approved', true)->where(' my_schema.posts.status', 2)->where(' my_schema.posts.deleted', null) )->toSql();

Example:

User::joinRelationship('posts', fn ($join) => $join->where('approved', true)->where('status', 2)->where('deleted', null) )->toSql();

Result:

SELECT my_schema.users.* FROM my_schema.users JOIN my_schema.posts ON my_schema.posts.user_id = my_schema.user.id AND my_schema.posts.approved = 1 AND my_schema.posts.status = 2 AND my_schema.posts.deleted IS NULL
This makes it easier to avoid having to cite the table name in each where statement of the connection.
This avoids conflicts when 2 tables have one or more columns with the same name.


2 - Add the behavior of ( joinRelationship ) equal to ( joinRelationshipUsingAlias ​​), but with the following behavior:

A: In joinRelationship, if there are 2 parameters, the first is the relationship and the second is the anonymous function containing the WHERE clause.

B: In joinRelationship, if there are 3 parameters, the first is the relationship, the second is an alias to be assigned to the relationship, and the third is the anonymous function containing the WHERE clause.

Example:

User::joinRelationship('posts', 'test', fn ($join) => $join->where('test.approved', true)->where('test.status', 2)->where('test.deleted', null) )->toSql();

Result:

SELECT my_schema.users.* FROM my_schema.users JOIN my_schema.posts as test ON test.user_id = my_schema.user.id AND test.approved = 1 AND test.status = 2 AND test.deleted IS NULL

This helps in the flexibility of handling the join connection.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant