laravel el equent问题当我使用where()之前()

发布于 2025-02-13 19:13:30 字数 695 浏览 0 评论 0原文

在Laravel 8中,当我使用wery()之前()初始化条件之前使用Where()时!

例如;

$post = Post::find($id);
$post->update(['status' => 'published']); 

此更新查询是:

update `posts` set `status` = 'published' where `id`='1' 

但是当我使用where():

$post = Post::find($id);
$post->where('status', '=', 'unpublished')->update(['status' => 'published']); 

更新查询时:

update `posts` set `status` = 'published' where `status`='unpublished' 

但是我希望它像以下内容,

update `posts` set `status` = 'published' where `status`='unpublished' and `id`='1' 

为什么这样的查询是这样的?我的期望错了吗?还是错误?

In laravel 8 and elequent when i use where() before update() initialize condition ereased!

for example;

$post = Post::find($id);
$post->update(['status' => 'published']); 

this update query is:

update `posts` set `status` = 'published' where `id`='1' 

but when i use where():

$post = Post::find($id);
$post->where('status', '=', 'unpublished')->update(['status' => 'published']); 

update query is:

update `posts` set `status` = 'published' where `status`='unpublished' 

But I expect it to be like the following

update `posts` set `status` = 'published' where `status`='unpublished' and `id`='1' 

Why is the query like this? Is my expectation wrong? or is bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

多情癖 2025-02-20 19:13:34

这是因为您在收集上使用,而不是构建新查询。

如果要使用特定属性值更新当前$ post,则需要使用而不是。

$post = Post::find($id);

if ($post->status == 'unpublished')
    $post->update(['status' => 'published']); 

It's because you are using where on a Collection instead of building a new query.

If you want to update the current $post with a specific value of an attribute you need to use if instead.

$post = Post::find($id);

if ($post->status == 'unpublished')
    $post->update(['status' => 'published']); 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文