gpt4 book ai didi

mysql - Laravel 4 将数据库字段更新为空 - 错误

转载 作者:太空宇宙 更新时间:2023-11-03 10:50:27 25 4
gpt4 key购买 nike

这段代码给我一个错误:

$post = Post::find($post_id);
$post->deleted_at = null;
$post->save();

这是错误:

Creating default object from empty value

也试过

$post->deleted_at = '';

这给了我同样的错误。

最佳答案

如果它没有找到您在该变量中有 null 的模型,那么您可以:

$post = Post::findOrNew($post_id); /// If it doesn't find, it just creates a new model and return it

$post->deleted_at = null;

$post->save();

但如果你真的需要它存在:

$post = Post::findOrFail($post_id); /// it will raise an exception you can treat after

$post->deleted_at = null;

$post->save();

而且,如果这无关紧要:

if ($post = Post::find($post_id)) 
{
$post->deleted_at = null;

$post->save();
}

关于mysql - Laravel 4 将数据库字段更新为空 - 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25577212/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com