It is possible in Laravel to store JSON for a new model with relation models (existing or new) in one save()
/create()
method?
For example:
{
"name": "Name of new Model A record",
...
"comments": [
{
"id": 123, # ID of existing record of Model B
},
{
"name": "Create new Model B record"
}
]
}
Code for writing:
ModelA::create([
"name" => "Name of...",
"comments" => [
["id" => 123],
["name" => "Create new..."],
],
]);
Method ModelA::create()
/ModelA::save()
write to database just record of Model A. Record of ModelB not created.
Any errors.
Relations in models are correct.
P.S. I using UUID as primary and foreign keys.
What do you mean “store JSON for a new model“, what does store JSON mean in code? Aren’t you using
Model::create(...)
or similar?Yes, I use
Model::create()
.And what are you doing? share code, I honestly do not understand what you mean, are you getting an error? how are you trying to store this info?
Which DB system do you use? MongoDB?
@matiaslauriti Updated.
Show 6 more comments