我本周已将我的laravel应用程序php版本升级到php 7.2,从那时起我在laravel应用程序中遇到了大问题。在将PHP升级到7.2之前,每件事都很有效。
主要问题是关于count()和array_merge()函数抛出此错误:
对于array_merge()
函数,代码如下:
$array = array_merge(
$model->toSearchableArray(), $model->scoutMetadata()
);
if (empty($array)) {
return;
}
ErrorException·array_merge():参数#1不是数组。
当模型没有返回任何记录并返回null时,我在此代码中遇到count()
错误:
count(TutorialReview::where('TutorialID', 5)->where('UserID', 6)->get())
count()
:参数必须是实现Countable的数组或对象。
我的laravel版本是5.4
现在我的问题是如何解决问题,并升级到laravel 5.5解决任何问题?
在PHP 7.2中,在以下RFC中更改了count()
行为:https://wiki.php.net/rfc/counting_non_countables
但你可以在laravel中使用->count()
来计算,这里有一个例子:
$count = TutorialReview::where('TutorialID', 5)->where('UserID', 6)->get()->count();
这样您就可以获得总记录数。
只需在@
之前添加count
。 I.E.
@count(object or array);
要解决array_merge()问题,请尝试以下步骤:
return ['source' => null, 'maxLength' => null, 'method' => null, 'separator' => '-', 'unique' => true, 'uniqueSuffix' => null, 'includeTrashed' => false, 'reserved' => null, 'onUpdate' => false, ];
php artisan config:cache
解决count()问题:Try This
count(): Parameter must be an array or an object that implements Countable.
实际上它不是一个错误,它是一个预期的行为。 Laravel 5.4或5.5与Php 7.2不完全兼容。 Count()行为只是在PHP 7.2 Look at this上改变
另一种方法是使用PHP 7.1或更低版本,直到修复兼容性问题。
试试这个:
$array = array_merge(
collect($model->toSearchableArray())->toArray(), $model->scoutMetadata()
);
在计算模型实例时,也可以通过->count()
而不是count()
来实现
只需在web.php中添加以下代码即可
if (version_compare(PHP_VERSION, '7.2.0', '>=')) {
// Ignores notices and reports all other kinds... and warnings
error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
// error_reporting(E_ALL ^ E_WARNING); // Maybe this is enough
}
在PHP 7.2计数方法我不可用由于安全原因你需要安装扩展名来查看该文档