New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Laravel not compatiable with php 7.2 #20248

Closed
morloderex opened this Issue Jul 25, 2017 · 23 comments

Comments

Projects
None yet
@morloderex
Copy link
Contributor

morloderex commented Jul 25, 2017

  • 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.
@ntzm

This comment has been minimized.

Copy link
Contributor

ntzm commented Jul 25, 2017

Good spot. This needs fixing:

https://3v4l.org/Efkoc

Was added to PHP 7.2 here: php/php-src@cd95326#diff-3054389ad750ce9a9f5895cd6d27800fR8183

@Dylan-DPC

This comment has been minimized.

Copy link
Contributor

Dylan-DPC commented Jul 25, 2017

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

@deleugpn

This comment has been minimized.

Copy link
Contributor

deleugpn commented Jul 25, 2017

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

@fernandobandeira

This comment has been minimized.

Copy link
Contributor

fernandobandeira commented Jul 25, 2017

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.

@Dylan-DPC

This comment has been minimized.

Copy link
Contributor

Dylan-DPC commented Jul 26, 2017

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

@themsaid

This comment has been minimized.

Copy link
Member

themsaid commented Jul 27, 2017

Moving the discussion to the already opened PR.

@andreshg112

This comment has been minimized.

Copy link

andreshg112 commented Feb 7, 2018

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.

@BenjaminHoegh

This comment has been minimized.

Copy link

BenjaminHoegh commented Feb 28, 2018

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

@vovanmai

This comment has been minimized.

Copy link

vovanmai commented Apr 11, 2018

Thank you very much . It is soved 👍

@Denn1992

This comment has been minimized.

Copy link

Denn1992 commented May 6, 2018

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 ?

@andreshg112

This comment has been minimized.

Copy link

andreshg112 commented May 6, 2018

@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().

@beingmohit

This comment has been minimized.

Copy link

beingmohit commented May 13, 2018

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);
        }
    }
@vikasrinvi

This comment has been minimized.

Copy link

vikasrinvi commented Jul 30, 2018

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.
@adelsedighi

This comment has been minimized.

Copy link

adelsedighi commented Aug 25, 2018

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

@octoxan

This comment has been minimized.

Copy link

octoxan commented Sep 6, 2018

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

@zachleigh

This comment has been minimized.

Copy link

zachleigh commented Oct 14, 2018

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.

@DarrenChowles

This comment has been minimized.

Copy link

DarrenChowles commented Oct 18, 2018

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?

@mfn

This comment has been minimized.

Copy link
Contributor

mfn commented Oct 18, 2018

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.

@DarrenChowles

This comment has been minimized.

Copy link

DarrenChowles commented Oct 18, 2018

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.

@mfn

This comment has been minimized.

Copy link
Contributor

mfn commented Oct 18, 2018

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.

@imizallah

This comment has been minimized.

Copy link

imizallah commented Nov 13, 2018

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: ishayasunday@gmail.com.
I can email you the entire project.

@andreshg112

This comment has been minimized.

Copy link

andreshg112 commented Nov 13, 2018

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 {

	// ...
	
}
@MibuSawatori

This comment has been minimized.

Copy link

MibuSawatori commented Dec 6, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment