laravel 解决laravel 5.4应用升级到php 7.2的问题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/47827268/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 17:06:49  来源:igfitidea点击:

Solve issue of upgrading to php 7.2 in laravel 5.4 application

phplaravellaravel-5.4laravel-5.5

提问by atieh mokhtary

I have Upgraded my laravelapplication php version to php 7.2this week and from then I am facing big problems in my laravelapplication. before upgrading php to 7.2every thing worked pefectly.

本周我已将我的Laravel应用程序 php 版本升级到php 7.2,从那时起我在我的Laravel应用程序中遇到了大问题。在将php升级到 7.2之前一切正常。

the main issue is about count()and array_merge()functions which is throwing this error:

主要问题是关于引发此错误的count()array_merge()函数:

for array_merge()function the code is as below:

array_merge()函数的代码是如下:

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

            if (empty($array)) {
                return;
            }

ErrorException · array_merge(): Argument #1 is not an array.

ErrorException · array_merge():参数 #1 不是数组。

and I am facing count()error for example at this code when the model returns no records and returns null:

count()例如,当模型不返回记录并返回空值时,我在此代码中遇到错误:

count(TutorialReview::where('TutorialID', 5)->where('UserID', 6)->get())

count(): Parameter must be an array or an object that implements Countable.

count(): 参数必须是一个数组或一个实现 Countable 的对象。

my laravel version is 5.4

我的 Laravel 版本是5.4

now my question is how can I solve the issues, and does upgrading to laravel 5.5solve any of the issues?

现在我的问题是如何解决这些问题,升级到Laravel 5.5 是否可以解决任何问题?

回答by Bhavin Solanki

In PHP 7.2changed count()behavior in the following RFC: https://wiki.php.net/rfc/counting_non_countables

PHP 7.2 中更改count()了以下 RFC 的行为:https: //wiki.php.net/rfc/counting_non_countables

But you can get count using ->count()in laravel, here is an example of it:

但是你可以->count()laravel 中使用计数,这是一个例子:

$count = TutorialReview::where('TutorialID', 5)->where('UserID', 6)->get()->count();

This way you can get total records count.

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

回答by Amos Chihi

Just add @before count. I.E.

@之前添加count。IE

@count(object or array);

回答by Minar Mnr

To solve array_merge() issue, try those steps:

要解决 array_merge() 问题,请尝试以下步骤:

  1. sluggable.php config file at app/config with data

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

  2. Execute the command, php artisan config:cache

  1. 带有数据的 app/config 中的 sluggable.php 配置文件

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

  2. 执行命令, php artisan config:cache

To solve count() issue: Try This

解决 count() 问题试试这个

count(): Parameter must be an array or an object that implements Countable.

Actually its not a error , its an expected behavior . Laravel 5.4 or 5.5 is not fully compatible with Php 7.2 . Count() behaviour just change at PHP 7.2 Look at this

实际上它不是错误,而是预期的行为。Laravel 5.4 或 5.5 与 PHP 7.2 不完全兼容。Count() 行为只是在 PHP 7.2 中改变 看看这个

Another way just use PHP 7.1 or below until compatibility issue fixed.

另一种方法是使用 PHP 7.1 或更低版本,直到兼容性问题得到解决。

回答by DorienCragen

try this:

尝试这个:

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

also when counting model instance do this by ->count()instead of count()

也在计算模型实例时这样做->count()而不是count()

回答by Rahul Tathod

just add below code in web.php

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

回答by Sid Heart

in PHP 7.2 Count Method i not available due to security reason you need to install extension for that look at that documentation

在 PHP 7.2 Count Method 中,由于安全原因,您需要安装扩展以查看该文档

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

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