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

$model->seo->update() doesn't create record in seo table

Open DaniWinter opened this issue 1 year ago • 1 comments

$post = Post::find(1);

$test = $post->seo->update([
   'title' => 'My great post',
   'description' => 'This great post will enhance your live.',
]);

Test returns false and doesn't insert row into seo table.

However the following does work.

$post = Post::find(1);
$seo = $post->seo;
$seo->title = 'My great post;
$seo->description = 'This great post will enhance your live.';
$seo->save();

Laravel version: 10.25.2 PHP version: 8.1.17

DaniWinter avatar Dec 14 '23 08:12 DaniWinter

Hi, thanks for your comment! That's strange, since the SEO model will be created using the created event callback. In your first code snippet, can you do a dd($post->seo, $post->seo->exists) after the $post = Post::find(1) part?

My main hypothesis is that the static::created(fn (self $model): self => $model->addSEO()); is not working in your tests. Therefore, the ->seo() relation returns a default, unsaved empty model. If you try to update() an Eloquent model that does not exist, then Laravel will simply return. However, save() will work, since that will persist the model as well.

Can you check if you created the post quietly or if you faked (Post-related) events in your tests in some way?

ralphjsmit avatar Dec 15 '23 21:12 ralphjsmit

Hi, closing this since I'm cleaning up some issues and didn't receive a reply anymore.

ralphjsmit avatar Jun 15 '24 13:06 ralphjsmit