mardi 13 octobre 2015

Laravel - How to get data like hasOne in a many-to-many relationship

Here are my tables having a many-to-many relationship and group_user as the intermediate table.

| users     | groups      | group_user    |
-------------------------------------------
| id        | id          | group_id      |
| name      | name        | user_id       |
| created_at| creator_id  | created_at    |
| updated_at| created_at  | updated_at    |
|           | updated_at  |               |

groups.creator is the user id referenced from users table.

When I try to query the group table, it returns something like this.

[{"id":1,"name":"Akatsuki","creator":1,"created_at":"2015-10-13 12:22:20","updated_at":"2015-10-13 12:22:20"}]

What I want is when I query the groups table, it also returns the user data of the creator like how one-to-many relationships do.



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire