Skip to content

Embed existing document into newly created document #2232

@dariel87

Description

@dariel87
  • Laravel-mongodb Version: 8.34.0
  • PHP Version: 8.0
  • Database Driver & Version: 3.8

I have 2 models, Product and Vendor. when I want to create a new product, I need to embed existing vendor to my new product. so far, this is what I have done:

$vendor = Vendor::find($request->vendor);
$product = Product::create([
    'name'=>$request->name
    'vendor'=>$vendor
]);

that code resulting the following document

{
    "_id": "606ffac287be2020b968c613",
    "name": "product name",
    "vendor": {
        "incrementing": true,
        "exists": true,
        "wasRecentlyCreated": false,
        "timestamps": true
    },
    "updated_at": "2021-04-09T06:57:06.032000Z",
    "created_at": "2021-04-09T06:57:06.032000Z"
},

so, what is the correct way to embed existing document into newly created document?
PS: I also tried to save the vendor just like the documentation said, that is:

$product->vendor()->save($vendor)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions