jeudi 13 septembre 2018

How to sotre an id from a collection on to another collection of MongoDB in Laravel

I have two Collection category and subcategory. One category can have 0 or more subcategories that's why i need relation between them how can i store _id oa document form category collection to subcategory collection's document.

category

{
    "_id" : ObjectId("5b8abe366a44fa2d080065d2"),
    "Title" : "Aptitude",
    "Description" : "Aptitude",
    "Slug" : "aptitude",
    "updated_at" : ISODate("2018-09-01T16:28:38Z"),
    "created_at" : ISODate("2018-09-01T16:28:38Z")
 }

subcategory

{
    "_id" : ObjectId("5b9abbd06a44fa207c001674"),
    "Title" : "Aithmatic Apptitude",
    "Description" : "Aithmatic Apptitude",
    "Slug" : "arithmatic_aptitude",
    "CategoryId" : "5b8abe366a44fa2d080065d2",
    "updated_at" : ISODate("2018-09-13T19:34:40Z"),
    "created_at" : ISODate("2018-09-13T19:34:40Z")
}
{
    "_id" : ObjectId("5b9ac7e86a44fa207c001678"),
    "Title" : "Reasoning",
    "Description" : "Reasoning",
    "Slug" : "reasoning",
    "CategoryId" : "5b8abe366a44fa2d080065d2",
    "updated_at" : ISODate("2018-09-13T20:26:16Z"),
    "created_at" : ISODate("2018-09-13T20:26:16Z")
}

As you can see i am storing the category_id on subcategory collection a string "CategoryId" : "5b8abe366a44fa2d080065d2",

Here is my code for storing

$subcategory = Subcategory::create(
    [
        'Title' => $request->Title, 
        'Description' => $request->Description,
        'Slug' => $request->Slug,
        'CategoryId' =>  $request->CategoryId,
    ]

);

Want to sore it like actual reference"category_id" ObjectId("5b8abe366a44fa2d080065d2") . convert this command db.subcategory.insert({Title:"Example",Description:"Example", Slug:"Example",CategoryId:db.category.find({_id:"5b9abc8cb3562ec4e990ec5b"})._id}) how can it be possible, Please help



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire