Framework: Laravel not compatiable with php 7.2

Created on 25 Jul 2017  ·  29Comments  ·  Source: laravel/framework

  • Laravel Version: 5.4.30 or 5.5-dev
  • PHP Version: php 7.2.0beta1
  • Database Driver & Version: not relevent

Description:

Hello all.

Today i decided to try out php 7.2, but i stumbled apon a pretty big issue invovling laravel 5.5 (dev-master).. I kept recieving an exception if type count(): Parameter must be an array or an object that implements Countable. I pulled down the framework core and found out that pretty much all tests where failing on php 7.2.

I'm not sure it's totally due to this new warning for count() but it seems very likey to be.

Steps To Reproduce:

  1. Upgrade your php version to php 7.2.0beta1 (as of filling in this issue)
  2. Pull down the framework core package.
  3. run it's tests and see the error.

Most helpful comment

I solved the problem putting the next code at the beginning of the controller:

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
}

If you don't know what's the controller where you have the issue, you can put the code at the beginning of routes/web.php
However, I don't recommend this alternative because all notices and warnings should be solved.

All 29 comments

Good spot. This needs fixing:

https://3v4l.org/Efkoc

Was added to PHP 7.2 here: https://github.com/php/php-src/commit/cd953269d3d486f775f1935731b1d6d44f12a350#diff-3054389ad750ce9a9f5895cd6d27800fR8183

It may not be considered a bug till 7.2 is "released". I think its better that laravel mentions 7.0 || 7.1 for 5.5

I think Laravel 5.5 (being LTS) will have to support 7.2.

Yep, I've created #20253 to add php7.2 to travis on master so we can start working on that, it seems it's only this warning that's failing.

@deleugpn PHP 7.2 is unstable at the point of 5.5 release. So cannot be supported

Moving the discussion to the already opened PR.

I solved the problem putting the next code at the beginning of the controller:

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
}

If you don't know what's the controller where you have the issue, you can put the code at the beginning of routes/web.php
However, I don't recommend this alternative because all notices and warnings should be solved.

There are multiple issue with Laravel and php 7.2 a example is that its have problem with image upload

Thank you very much . It is soved :+1:

This is still an issue in Laravel Framework 5.6.20 with PHP 7.2.3? Is there already a way to fix this instead of the hacky way from @andreshg112 ?

@Denn1992, this is not an error with Laravel itself. It's a characteristic for PHP 7 (http://php.net/manual/en/function.count.php#refsect1-function.count-examples). You should modify your code so the thing that you are trying to count can be countable, or maybe using empty().

Quick Hack

Disable warning in error_reporting and edit handleError function to not throw error

vendor/laravel/framework/src/Illuminate/Foundation/Bootstrap/HandleExceptions.php:56

 public function handleError($level, $message, $file = '', $line = 0, $context = [])
    {
        if (error_reporting() & $level) {
            if (strpos($message, 'Parameter must be an array or an object that implements Countable') !== false) 
                return;

            throw new ErrorException($message, 0, $level, $file, $line);
        }
    }

I have the same problem when I am using Laravel 5.2, PHP 7.2

I changed error Line 1185 in 'vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php' with:
$originalWhereCount = is_array($query->wheres) ? count($query->wheres) : 0;

  • Note
    Its works for me but I recommend that to switch php version as making changes to vendor folder is never ever a good option.

beingmohit commented on May 13 •
this worked for me thank you

Running into this error just running SomeClass::get() in 7.2 o.O

I got this error in a Laravel 5.3 project when using a Model::all(). Was coming from Illuminate\Database\Eloquent\Builder line 1231:

$originalWhereCount = count($query->wheres);

Quick fix was to add a where clause that always returns true to my query:

User::whereNotNull('id')
    ->get()
    ->sortBy('order')
    ->values();

A little hacky, but will do until I have time to upgrade the app.

Having this issue on 5.5 LTS and PHP 7.2 despite the 5.5 LTS docs saying:

PHP >= 7.0.0

It seems with 5.5 LTS you'll need to stick to PHP 7.1?

Having this issue on 5.5 LTS and PHP 7.2 despite the 5.5 LTS docs saying:

This was likely written before issues with 7.2 were known.

That said: if you have anything specific to share and since according to https://laravel-news.com/laravel-release-process (is this "official"?) it's in "bug fix" state, I don't see why things could be improved.

This was likely written before issues with 7.2 were known.

That said: if you have anything specific to share and since according to https://laravel-news.com/laravel-release-process (is this "official"?) it's in "bug fix" state, I don't see why things could be improved.

Here is the more official release schedule: https://laravel.com/docs/5.7/releases#support-policy

I ran into this issue doing a count() on a query where there were no results. This is because since PHP 7.2, count(null) will return a warning instead of 0.

I can see why it might not have been fixed - changing Laravel to return an empty array instead would work, but would break implementations that check for null explicitly.

Nice, thanks for the correct link 👍

I ran into this issue doing a count() on a query where there were no results. This is because since PHP 7.2, count(null) will return a warning instead of 0.

If it's in the framework, I think it can be easy fixed/backported if you create an issue with more details.

I am new to Laravel and i have a Laravel 4 project that works fine with PHP 5.6 but shows many errors with PHP 7.2. I really need help with it. My email is: [email protected].
I can email you the entire project.

Hi, @imizallah! You can try this in app/controllers/YourController.php:

<?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
}

class YourController extends \BaseController {

    // ...

}

Hi, @imizallah! You can try this in app/controllers/YourController.php:

<?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
}

class YourController extends \BaseController {

  // ...

}

I'm using Laravel 5.2 and use this in routes.php. It solves my problem.

I don't want to suppress warnings...is there a plan to bring PHP 7.2 compatibility to Laravel 5.5? Or a later version?

5.5 and above supports PHP 7.2

And 7.3, notwithstanding a 7.3 PHP bug that has caused some problems. A fix is in process if it hasn't already been patched.

Instead of "count" you can use "empty" function to check the contents.

Eg:
Instead of:
if ( count( $data ) )

Use:
if ( ! empty( $data ) )

Hope this helps some one.

Thank you it's solved by putting this on the controller after the namespace
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 }

I am getting this issue in laravel 5.8 running php v7.2.22

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ghost picture ghost  ·  3Comments

YannPl picture YannPl  ·  3Comments

CupOfTea696 picture CupOfTea696  ·  3Comments

Fuzzyma picture Fuzzyma  ·  3Comments

kerbylav picture kerbylav  ·  3Comments