Laravel-mongodb: Embedded Model Not Saving

Created on 1 Jul 2015  路  5Comments  路  Source: jenssegers/laravel-mongodb

Can anyone advise why this isn't working:

// get a user
$user = $this->user->all()->first();
// Create a new pool
$pool = $this->pool->create([
    'name' => $data['name']     
]);
// Embed the user
$pool->user()->save($user);

The $pool model saves without the user for some reason. My Pool model:

public function user()
{
    return $this->embedsOne('Acme\User\User');
}

I don't understand why the user is not embedded in the Pool document?

Most helpful comment

try this:

// Embed the user
$pool->user()->associate($user);
$pool->save();

All 5 comments

try this:

// Embed the user
$pool->user()->associate($user);
$pool->save();

cheers!

@webular @nicklee1990 so after having the same issue as this ticket, and #968 as well, i've found that using ->associate() is the only way this works for me as well.

Why does the readme.md example then say to use ->save() when it's clearly not doing anything? Why not get ->save() working or change the readme? This way the next person that wants to try out ->embedsOne doesn't have to find this issue or #968 to get it working.

2019 and this problem still exists!!!!!!!!

@rt3norio, maintaner @jenssegers doesn't have enough time to support library, so community can help and prepare PR's if problem really exists. As possible solving @jenssegers can add least maintainer which have enough time for supporting and improvement of this library.

Was this page helpful?
0 / 5 - 0 ratings