首页 文章

解决在laravel 5.4应用程序中升级到php 7.2的问题

提问于
浏览
6

我本周已将我的 laravel 应用程序php版本升级到 php 7.2 ,从那时起,我在我的 laravel 应用程序中遇到了大问题 . 在升级 php to 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 解决任何问题?

6 回答

  • 0

    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();
    

    这样您就可以获得总记录数 .

  • 1

    只需在 count 之前添加 @ 即可 . I.E.

    @count(object or array);
    
  • 5

    To solve array_merge() issue ,尝试以下步骤:

    具有数据的app / config上的

    • sluggable.php配置文件

    return ['source'=> null,'maxLength'=> null,'method'=> null,'separator'=>' - ','unique'=> true,'uniqueSuffix'=> null,'includeTrashed'= > false,'reserved'=> null,'onUpdate'=> false,];

    • 执行命令, php artisan config:cache

    To solve count() issueTry 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或更低版本,直到修复兼容性问题 .

  • 4

    试试这个:

    $array = array_merge(
        collect($model->toSearchableArray())->toArray(), $model->scoutMetadata()
    );
    

    在计算模型实例的同时通过 ->count() 而不是 count() 执行此操作

  • 0

    只需在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
    }
    
  • -1

    在PHP 7.2计数方法我不可用由于安全原因你需要安装扩展名来查看该文档

    https://wiki.php.net/rfc/counting_non_countables

相关问题