在 Aravel 中转换 Carbon::now() toDateTimeString() 时出现问题



在我的代码中,我有这样的部分。

->whereDate('published_at', '<=', (Carbon::now())->toDateTimeString())

这个查询在我的开发机器(家园(中运行良好,所以我将其推送到开发服务器(aws(。

但我有一个问题说。

语法错误,意外的"->"(T_OBJECT_OPERATOR(

但是,我解决了这个问题并发现了问题。我想像这样更改查询

->whereDate('published_at', '<=', Carbon::now()->toDateTimeString())

唯一的区别是不使用(Carbon::now())->toDateTimeString()),现在我使用没有括号的Carbon::now()->toDateTimeString())。现在在这两种环境中,我的代码都可以工作。

Homsetead 和 AWS 中的 PHP 版本如下:

宅基地php -v产量

PHP 7.1.0-2+deb.sury.org~xenial+1 (cli) ( NTS )

AWS 上的php -v输出

PHP 5.6.29 (cli) (built: Jan 18 2017 19:08:44) 

我只能想到发生此问题是由于 php 版本的差异。

但它让我感到困惑的是,删除周围的括号如何使我的代码工作。感谢您对此的意见。谢谢!

(Carbon::now())->toDateTimeString()是你永远不需要做的事情。我唯一会像这样使用括号的时候是如果我正在新上课。

Carbon::now()->toDateTimeString()是正确的方法。

话虽如此,使用 Laravels 查询构建器,您根本不需要添加toDateTimeString()

您应该能够:

->whereDate('published_at', '<=', Carbon::now())

希望这有帮助!

相关内容

  • 没有找到相关文章

最新更新