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.
Good spot. This needs fixing:
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;
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
Most helpful comment
I solved the problem putting the next code at the beginning of the controller:
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.