I'm first time using MongoDb with Laravel.
I have one EntityForm
Model as like below
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class EntityForm extends Eloquent
{
use HasFactory;
protected $connection = 'mongodb';
protected $collection = 'entityForm';
protected $fillable = [
'entity_name',
'entity_code'
];
public function form_fields()
{
return $this->embedsMany(EntityFormFields::class, 'form_fields');
}
}
this EntityForm
model has embedsMany Relationship with EntityFormFields
Model.
see my EntityFormFields
Model
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Jenssegers\Mongodb\Eloquent\Model as Eloquent;
class EntityFormFields extends Eloquent
{
use HasFactory;
protected $connection = 'mongodb';
public $incrementing = false;
protected $primaryKey = null;
public $timestamps = false;
protected $fillable = [
'input_name',
'input_type',
'input_label',
];
}
// this is my Model Query
$entity = EntityForm::first();
// var_dump($entity->form_fields());
$entity->form_fields()->create(['input_name' => "name", 'input_type' => "text", 'input_label' => "Name"]);
$entity->form_fields()->create(['input_name' => "age", 'input_type' => "number", 'input_label' => "Age"]);
Now, when I tries to create embed doc without primary key I'm getting undefined index": error message. but my entry saving as child embed doc into Db as I expected. see example in official docs .
you can see my Collection raw data
{
"_id" : ObjectId("63ecd3c392120a6def773e9c"),
"entity_code" : "student",
"entity_name" : "Student",
"updated_at" : ISODate("2023-02-15T12:44:51.492Z"),
"form_fields" : [
{
"input_name" : "name",
"input_type" : "text",
"input_label" : "Name"
},
{
"input_name" : "age",
"input_type" : "number",
"input_label" : "Age"
}
]
}
If I removing the public $incrementing = false; protected $primaryKey = null;
lines from EntityFormFields
Model then it doesn't throw any exception error.
My Issue :- How to we can save embed Doc without any Primary key like _id
?
Doubt :- in case if we save embed doc with any primary key than can we do update or delete this embed doc directly without getting or affecting the value of parent doc?
Please guide me about the how to we can manage Child Nested embed Docs in Laravel Model