php - laravel mongodb nested collection relationship -


i using laravel mongodb collection in have relationship issues here collection of user in user have multiple addresses , addresses save in collection , ids save in user collection

{  "_id":objectid("52ffc33cd85242f436000001"),  "contact": "987654321",  "dob": "01-01-1991",  "name": "tom benzamin",  "address_ids": [   objectid("52ffc4a5d85242602e000000"),   objectid("52ffc4a5d85242602e000001")     ] } 

for getting user information , addresses information have make 2 query 1 user , other addresses

can possible user info addresses in single query using eloquent relationship. sorry new in mongodb collection.

you don't need save document id's of addresses in user document trying achieve. laravel clean code, ain't clean.

make following relationships:

  • user > hasmany > addresses

  • addresses > belongsto > user

to load user object in laravel including address objects use eager loading (https://laravel.com/docs/5.3/eloquent-relationships#eager-loading).

you can write eloquent query followed in php:

user::where(...some condition...)->with('addresses')->get(); 

best regards,

bdschr


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -