Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Embed existing document into newly created document #2245

Unanswered
dariel87 asked this question in Q&A
Discussion options

  • 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)

You must be logged in to vote

Replies: 0 comments

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
1 participant
Converted from issue

This discussion was converted from issue #2232 on April 30, 2021 10:00.

AltStyle によって変換されたページ (->オリジナル) /