laravel-mongodb icon indicating copy to clipboard operation
laravel-mongodb copied to clipboard

Creating row through relationship method not working when queried with "with" method

Open lukasdev opened this issue 4 years ago • 0 comments

  • Lumen version: 8.0
  • PHP Version: 7.4

Description:

When I try to create a new row in the database through another object's relationship and that object was queried using the "with" method id doesn't work. Actually my new row ends up in another field of the queried object. I'll try to demonstrate an example:

class Model1 { public function relationshipOne() { return $this->embedsMany(Model2::class); }

 public function relationshipTwo(){
       return $this->embedsOne(Model3::class);
 }

}

If I now to something like this:

$object = Model1::with(['relationshipOne','relationshipTwo'])->where('_id', 'some_id_here')->first();

and then try to create a new row through a relationship method:

$object->relationshipOne()->create([ 'field_one' => 'val1', 'field_two' => 'val2' ]);

the expected new row doesn't show up in the database, it actually shows up in another random relationship field of the $object in the database.

lukasdev avatar Jul 09 '21 17:07 lukasdev