Monday, February 27, 2017

laravel field with array of ids map with names from another collection

I am using Laravel 5.4 and Mongodb This is the users collection in which there is "projects_alloted" that I need to map with projects collection
{
        "_id" : ObjectId("58b4120757a572212c0033c9"),
        "name" : "Sree",
        "email" : "erer@dfdfb.com",
        "mobile" : "4343222111",
        "password" : "$2y$10$ycErDI8Ej8A11GOWofDpI.x3DyPGRZerKEy7VmkZrpe/1A0v0Crpq", 
        "designation" : "Gbgb",
        "role" : "Director",
        "active" : 1,
        "projects_alloted" : [
                "58b039b057a5721f0c00629d",
                "58b2b4ac57a57223b000733f",
                "58b0289657a5721f0c00629b"
        ],
        "company_master_id" : "58ac477331d2ae7aa44f8349"
}

I need to map "projects_alloted" ids with ids from project collection to get the project name
{
        "_id" : ObjectId("58b0266a57a5721f0c00629a"),
        "project_name" : "Wallis",
        "project_location" : "Mumbai",
        "project_type" : "Under Construction",
        "project_price_low" : "20000000",
        "project_price_high" : "80000000",
        "project_file" : null,
        "company_master_id" : "58ac477331d2ae7aa44f8349"
}
{
        "_id" : ObjectId("58b0289657a5721f0c00629b"),
        "project_name" : "Marine Enclave",
        "project_location" : "Mumbai",
        "project_type" : "Under Construction",
        "project_price_low" : "2000000",
        "project_price_high" : "4000000",
        "project_file" : "Appointment Reciept fatema.pdf",
        "company_master_id" : "58ac477331d2ae7aa44f8349"
}

I want to display on view list of users along with the project name given to them. Please suggest how I should proceed. I have tried using helpers function to get name of the project on view but its giving me error and since I am new to laravel I don't know whats the right way to do this. All help will be appreciated. Thankyou.


from Latest question asked on Laravel tag.


via Sanjana Anand

Advertisement