Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solve issue of upgrading to php 7.2 in laravel 5.4 application

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

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

for array_merge() function the code is as below:

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

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

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

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

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

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

my laravel version is 5.4

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

like image 998
atieh mokhtary Avatar asked Dec 15 '17 06:12

atieh mokhtary


2 Answers

In PHP 7.2 changed count() behavior in the following 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 = TutorialReview::where('TutorialID', 5)->where('UserID', 6)->get()->count();

This way you can get total records count.

like image 199
Bhavin Solanki Avatar answered Oct 24 '22 01:10

Bhavin Solanki


Just add @ before count. I.E.

@count(object or array);
like image 31
Amos Chihi Avatar answered Oct 23 '22 23:10

Amos Chihi