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

with_ relation gives un-wanted data. #477

Closed
yubarajshrestha opened this issue Jul 19, 2021 · 4 comments · Fixed by #478
Closed

with_ relation gives un-wanted data. #477

yubarajshrestha opened this issue Jul 19, 2021 · 4 comments · Fixed by #478
Labels
bug An existing feature is not working as intended

Comments

@yubarajshrestha
Copy link
Contributor

yubarajshrestha commented Jul 19, 2021

Describe the bug
When getting relationship result using with_ it gives wrong information about related records.

To Reproduce

  1. Create User Model and a Role Model
  2. Create another table role_user as a pivot table for user and role for many to many relationship.
  3. Now add few records on both.
  4. users = User.with_("roles").first()

Current output

{
        "id": 4,
        "code": "USER00004",
        "name": "Admin User",
        "email": "email-id-here",
        "phone": null,
        "status": "active",
        "status_remarks": null,
        "remember_token": null,
        "profile_image": null,
        "verified_at": null,
        "created_at": "2021-07-19T08:55:23+00:00",
        "updated_at": "2021-07-19T08:55:23+00:00",
        "role": {
            "id": 4,
            "name": "Admin",
            "slug": "admin",
            "status": "active",
            "created_at": "2021-07-19T14:28:11.173605+00:00",
            "updated_at": "2021-07-19T14:28:11.173605+00:00",
            "user_id": 4,
            "role_id": 3,
            "pivot": {
                "user_id": 4,
                "role_id": 3,
                "id": 4
            }
        },

Expected output

 {
        "id": 4,
        "code": "USER00004",
        "name": "Admin",
        "email": "email-id-here",
        "phone": null,
        "status": "active",
        "status_remarks": null,
        "remember_token": null,
        "profile_image": null,
        "verified_at": null,
        "created_at": "2021-07-19T08:55:23+00:00",
        "updated_at": "2021-07-19T08:55:23+00:00",
        "role": {
            "id": 3,
            "name": "Admin",
            "slug": "admin",
            "status": "active",
            "created_at": "2021-07-19T14:28:11.173605+00:00",
            "updated_at": "2021-07-19T14:28:11.173605+00:00",
           "pivot": {
                "user_id": 4,
                "role_id": 3,
                "id": 4
            }
        },

DIFF:

{
        "id": 4,
        "code": "USER00004",
        "name": "Admin User",
        "email": "email-id-here",
        "phone": null,
        "status": "active",
        "status_remarks": null,
        "remember_token": null,
        "profile_image": null,
        "verified_at": null,
        "created_at": "2021-07-19T08:55:23+00:00",
        "updated_at": "2021-07-19T08:55:23+00:00",
        "role": {
-             "id": 4,
+            "id": 3
            "name": "Admin",
            "slug": "admin",
            "status": "active",
            "created_at": "2021-07-19T14:28:11.173605+00:00",
            "updated_at": "2021-07-19T14:28:11.173605+00:00",
+            "user_id": 4,
+            "role_id": 3,
             "pivot": {
                "user_id": 4,
                "role_id": 3,
                "id": 4
            }
        },
@yubarajshrestha yubarajshrestha added the bug An existing feature is not working as intended label Jul 19, 2021
@Marlysson
Copy link
Contributor

Marlysson commented Jul 19, 2021

Shouldn't the user_id and role_id attributes returned in 'role' key, right?

@yubarajshrestha
Copy link
Contributor Author

Shouldn't the user_id and role_id attributes returned in 'role' key, right?

Role key should only contain the record from roles table. user_id and role_id is the part of pivot table i.e; role_user and should not be in role but in role.pivot.

@josephmancuso josephmancuso mentioned this issue Jul 24, 2021
3 tasks
@josephmancuso
Copy link
Member

Being fixed in #478

@yubarajshrestha
Copy link
Contributor Author

Being fixed in #478

Not fixed yet: comment

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An existing feature is not working as intended
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants