在 Laravel 中将 Carbon::now() 转换为 DateTimeString() 时出现问题
Issue when converting Carbon::now() toDateTimeString() in Laravel
在我的代码中有这样的部分。
->whereDate('published_at', '<=', (Carbon::now())->toDateTimeString())
此查询在我的开发机器 (homestead) 中运行良好,因此我将其推送到开发服务器 (aws)。
但是我遇到了一个问题。
syntax error, unexpected '->' (T_OBJECT_OPERATOR)
但是我解决了这个问题并找到了问题所在。我想将查询更改为
->whereDate('published_at', '<=', Carbon::now()->toDateTimeString())
唯一的区别是不再使用 (Carbon::now())->toDateTimeString())
,现在我使用 Carbon::now()->toDateTimeString())
而不使用括号。现在在这两种环境中我的代码都有效。
PHP Homsetead 和 Aws 中的版本如下,
php -v
在 Homestead 上的输出
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()
是您永远不需要做的事情。如果我是 newing-up a class,我唯一会像这样使用括号的情况。
Carbon::now()->toDateTimeString()
才是正确的方法。
话虽如此,使用 Laravels 查询构建器您根本不需要添加 toDateTimeString()
。
您应该能够:
->whereDate('published_at', '<=', Carbon::now())
希望对您有所帮助!
在我的代码中有这样的部分。
->whereDate('published_at', '<=', (Carbon::now())->toDateTimeString())
此查询在我的开发机器 (homestead) 中运行良好,因此我将其推送到开发服务器 (aws)。
但是我遇到了一个问题。
syntax error, unexpected '->' (T_OBJECT_OPERATOR)
但是我解决了这个问题并找到了问题所在。我想将查询更改为
->whereDate('published_at', '<=', Carbon::now()->toDateTimeString())
唯一的区别是不再使用 (Carbon::now())->toDateTimeString())
,现在我使用 Carbon::now()->toDateTimeString())
而不使用括号。现在在这两种环境中我的代码都有效。
PHP Homsetead 和 Aws 中的版本如下,
php -v
在 Homestead 上的输出
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()
是您永远不需要做的事情。如果我是 newing-up a class,我唯一会像这样使用括号的情况。
Carbon::now()->toDateTimeString()
才是正确的方法。
话虽如此,使用 Laravels 查询构建器您根本不需要添加 toDateTimeString()
。
您应该能够:
->whereDate('published_at', '<=', Carbon::now())
希望对您有所帮助!