7

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?

Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46
atieh mokhtary
  • 253
  • 2
  • 5
  • 18

5 Answers5

5

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.

Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46
  • 1
    thanks, it seems this is the solution for the count issue. my problem was because each time after get() function I used ->first() and that in count() function would gives the error. – atieh mokhtary Dec 17 '17 at 12:48
  • @atiehmokhtary, if this is solution for your question, you can accept as answer. Thanks in advance – Bhavin Solanki Apr 19 '18 at 12:50
4

Just add @ before count. I.E.

@count(object or array);
Bhavin Solanki
  • 4,740
  • 3
  • 26
  • 46
Amos Chihi
  • 375
  • 3
  • 7
2

To solve array_merge() issue , try those steps:

  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

To solve count() issue : Try This

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

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

Minar Mnr
  • 1,376
  • 1
  • 16
  • 23
1

try this:

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

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

CodexVarg
  • 502
  • 1
  • 6
  • 20
0

just add below code in 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
}
Rahul Tathod
  • 348
  • 5
  • 12