Laravel Carbon\Carbon::setLastErrors()

25

我有一个基于Laravel的API托管在Sharefd主机Siteground上,我进行了一些更改,例如将public文件夹更改为public html,并更新存储文件以便能够运行Laravel存储链接命令,但是当我尝试访问我的临时域时遇到了以下错误:

FULL ERROR NAME Carbon\Carbon::setLastErrors(): Argument #1 ($lastErrors) must be of type array, bool given, called in /home/customer/www/bassemb5.sg-host.com/vendor/nesbot/carbon/src/Carbon/Traits/Creator.php on line 98

enter image description here

这是我的index.php长成这样:

<?php

use Illuminate\Contracts\Http\Kernel;
use Illuminate\Http\Request;

define('LARAVEL_START', microtime(true));

/*
|--------------------------------------------------------------------------
| Check If The Application Is Under Maintenance
|--------------------------------------------------------------------------
|
| If the application is in maintenance / demo mode via the "down" command
| we will load this file so that any pre-rendered content can be shown
| instead of starting the framework, which could cause an exception.
|
*/

if (file_exists($maintenance = __DIR__.'/../storage/framework/maintenance.php')) {
    require $maintenance;
}

/*
|--------------------------------------------------------------------------
| Register The Auto Loader
|--------------------------------------------------------------------------
|
| Composer provides a convenient, automatically generated class loader for
| this application. We just need to utilize it! We'll simply require it
| into the script here so we don't need to manually load our classes.
|
*/

require __DIR__.'/../vendor/autoload.php';

/*
|--------------------------------------------------------------------------
| Run The Application
|--------------------------------------------------------------------------
|
| Once we have the application, we can handle the incoming request using
| the application's HTTP kernel. Then, we will send the response back
| to this client's browser, allowing them to enjoy our application.
|
*/

$app = require_once __DIR__.'/../bootstrap/app.php';
$app->bind('path.public', fn() => base_path('/public_html'));

$kernel = $app->make(Kernel::class);

$response = $kernel->handle(
    $request = Request::capture()
)->send();
$kernel->terminate($request, $response);

这是我添加的代码行,它完美地运作了:

$app->bind('path.public', fn() => base_path('/public_html')); 注:保留了HTML标签。
10个回答

53

嗨,还在管理我的旧网站。 您需要使用以下方式更新您的composer

composer update

一切正常运作


Hero。应该是任何 Laravel 问题的第一步,也适用于 Nova。这是最不侵入性的解决方案,并且评估速度最快。对我有用,再次感谢 Ahmed! - Huthaifa Muayyad

14

同时我在Siteground上管理我的旧网站时,客户联系我报告了500错误。

  • 当我查看日志时,没有发现任何问题。
  • 当我打开app_debug后,我们遇到了相同的错误。

这种情况在本地主机上不会发生,因为我安装了PHP 8.1。

如果您正在使用PHP 8.2并且出现此问题,则需要将composer.lock更新到最新的carbon版本。

https://github.com/briannesbitt/Carbon/releases/tag/2.62.1

或者将您的PHP版本降级至8.1

更新:这个错误是由Siteground自动更新您的PHP版本到8.2引起的。

我已经将我的PHP版本强制设置为8.1,但他们仍然自动管理并升级了PHP版本到8.2,没有任何通知或建议。


我正在使用 Laravel Sail。将 8.2 更改为 8.1 后,错误消失了。 - Ozal Zarbaliyev

14

我们遇到了和OP完全相同的错误(同一个文件和行号)。

经过我们的研究发现,它在2.57之后就出现了问题(2.57是正常工作的),对于我们来说,2.58已经出现故障并破坏了我们的流水线,在“composer install”阶段与完全相同的错误一起失败。

正如@tomexsans所提到的,它似乎在2.62.1+中得到了修复。今天我们升级到了最新可用版本(2.64),它可以正常工作,问题得到了解决。

--

简而言之

composer update nesbot/carbon
应该能行。

没有需要更新的内容! - undefined
没有需要更新的内容! - Luis Alfredo Serrano Díaz

11

我几天前也遇到了这个问题,只需在终端上输入composer update即可解决该问题。

之后请给此投票。


10

如果您无法将PHP版本更改为8.1,则可以更新此行

文件路径

./vendor/nesbot/carbon/src/Carbon/Traits/Creator.php

行号:

928

旧行

private static function setLastErrors(array $lastErrors)

新行

private static function setLastErrors($lastErrors)


2
只需运行以下命令即可:
composer update

它会起作用的。

1
我们使用碳和aporat-存储收据验证器的组合。不幸的是,由于版本硬锁和nesbot是另一个库的依赖项,我们无法使用composer更新nesbot。
幸运的是,当比较库Creator文件时,我们看到了最新版本中的热修复,那时是: https://github.com/briannesbitt/Carbon/blob/master/src/Carbon/Traits/Creator.php 最终,我们只更新了该特定文件来修复问题,因为只有setLastErrors()函数在那里更新了。
我真诚地希望它能帮助某些人。

我只是在这一行中删除了数组类型提示“private static function setLastErrors(array $lastErrors)”,改为“private static function setLastErrors($lastErrors)”,然后它就可以工作了。 - Aung Ko Man
这个修复的问题在于,当你忘记并运行composer update时,它会覆盖你已经更改的文件。请确保将此记录详细说明,以供未来的开发人员参考。 - tomexsans
完全同意@tomexsans的观点 - 即使Carbon已经修复了自己的错误,像Store receipt validator这样的库仍然需要更新其自身的Carbon依赖项。因此,不仅是未来的开发人员,我也确保通知aporat这个错误。 - Chris Baaijens

0
我刚刚通过将PHP版本从8.2降级到8.0.2来解决了这个问题。

你的回答可以通过提供更多的支持性信息来改进。请编辑以添加进一步的细节,例如引用或文档,以便他人可以确认你的回答是否正确。你可以在帮助中心找到更多关于如何撰写好回答的信息。 - undefined

0
这个问题出现在使用 PHP 8.2 及以上版本时。
在我的特定情况下,我通过简单地切换到 PHP 8.1 版本来解决了这个问题,这自动解决了这个问题。

0
确保您使用的是composer.json文件中指定的正确php版本。
要了解您的php版本,只需运行以下命令:
php -v

在Homestead中,例如,要更改7.4版本的php只需运行以下命令:
php74

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接