42

I'm facing strange case. I face an error in production env not while in dev it's working fine.

Development: Laravel 5.4.28 PHP 7.0.13 MYSQL 5.7.17

Production: Laravel 5.4.28 PHP 7.2.1 MYSQL 5.7.20

In implementation code. I used:

namespace App;
use Illuminate\Support\Facades\Storage;
use Laravel\Scout\Searchable;
use Illuminate\Database\Eloquent\Model;

class Artwork extends Model
{
  use Searchable;

In development it works fine. But in production it gives me this error: count(): Parameter must be an array or an object that implements Countable in Builder.php (line 936)

as you can see in this pic:

enter image description here

Any idea what is the reason behind this? and how to fix?

Catarina Ferreira
  • 1,824
  • 5
  • 17
  • 26
Khaled Al-Shehri
  • 421
  • 1
  • 4
  • 5

15 Answers15

72

Put this code at the beginning of your route file, it will work fine

if(version_compare(PHP_VERSION, '7.2.0', '>=')) {
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
}
cursorrux
  • 1,382
  • 4
  • 9
  • 20
  • 10
    OMG! What's going on with the development community nowadays? Hide the head in the sand is not a solution, fix the code is. Bad code IS bad code, despite you can hide the warnings or notices it generates. Do not use this method to "resolve" the issue. If you are using this it is because you updated your PHP in production without fully validating your code in a DEV/STAGE environment (bad move). However, if you tell me it is because your "shared hosting" updated their PHP to cause it in your site, you may use it as an emergency step while fixing the code, never beyond it! Then, change hosting!!! – Julio Marchi Feb 21 '20 at 17:08
  • 1
    @JulioMarchi It's happening exactly what's happening to the world. Hiding errors is simpler than fixing them. However, we use count($var) very often. I have used it 1653 times in one app code. The code breaks when the $var is null instead of being an array/object as expected. Fixing this inside 323 files isn't an easy thing either. Do I have another choice but to fix it? Not really if I want the app not breaking on my users. But still, hiding errors? – Adrian P. Apr 16 '22 at 01:10
  • 1
    This is called "Technical Debt", and the only way to deal with it is not to create it. In your case, YES, you are out of options. In fact, if you "assume" a variable will never be null (or unset) and recklessly call a count() function using it, then you most likely have much bigger problems all over your code beyond this example you gave... In terms of "Technical Debt", here you have a good article about the problem of allowing it to build: https://thedigitalinsider.com/youre-thinking-about-technical-debt-all-wrong/ – Julio Marchi Jun 03 '22 at 00:09
53

This is a documented change in PHP 7.2. You need to either update Laravel to 5.6 or downgrade PHP to version 7.1.

Ben Harold
  • 6,242
  • 6
  • 47
  • 71
  • But Laravel 5.3 has SoftDeletes but also supports lowest PHP 5.6 Does that mean, I shouldn't use soft delete in my package up to Laravel 5.6? – bmatovu Mar 22 '19 at 06:42
  • You should be using at least PHP 7.1 (as of March 2019). PHP 5.6 is no longer supported. This issue doesn't have anything to do with soft deletes as far as I can tell. We've had soft deletes since Laravel 4 and probably earlier. – Ben Harold Mar 22 '19 at 13:25
  • I know, but usecase kind complicated it. Am working on a package and I need to support older Laravel and PHP versions. For the moment I skipped soft deletes for the Laravel 5.3 - 5.5 which support lowest 5.6 - 7.0 – bmatovu Mar 22 '19 at 14:38
  • @BenHarold, may I ask you to take a look at a Laravel search related question here: https://stackoverflow.com/questions/76485513/laravel-search-with-multiple-keywords-against-multiple-columns-with-the-search ? – Istiaque Ahmed Jun 15 '23 at 22:06
17

Replace

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

by

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

in

\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php

  • 20
    **NEVER** modify files in the `vendor/` directory. – Ben Harold Sep 16 '19 at 22:46
  • Ok, but how do you push your changes ? it is ignored already – Mahmoud Abdelsattar Oct 25 '20 at 11:28
  • Old thread but this issue has been driving me nuts for 2 days. I wasn't noticing the problem on my remote server and php -v there was showing 7.2. On local machine I have php 7.2 so thought how can things be working on remote server if php 7.2 is being used. Turns out phpinfo shows 7.1. Didn't know the app and cli versions were different! So this Builder.php fix saved the day for now, though I realize better solution is upgrade laravel. But is php 7.2 really that much better than 7.1? – Brian Aug 26 '21 at 14:12
  • > But is php 7.2 really that much better than 7.1? Yes, it offers many improvements, although by the time I'm writing this, PHP 7.4 is almost EOL and you should be moving towards PHP 8.1 – Ben Harold Nov 16 '22 at 22:36
8

I was facing similar issue in Laravel 5.6. Where I was getting error for object based array. I knew that data in that particular variable will always remain object so i used to convert the object to array. Here is code sample: $objectData = (array)$objectData; echo "Total Elements in array are: ".count($objectData);

Farid Abbas
  • 294
  • 4
  • 5
8

My server was on PHP 7.1 when I updated to PHP 7.2 I got the same issue.

After searching I found why this occurs. (This occurs because of a PHP update.).

so in my case, the error is solved by typecasting.

I just update all code where I used to count

Before

//this is before     
count($adminDetails)

After updated

//after update all i typecast all the code where i used count
count((array)$adminDetails)

Goodluck

Arslan Ahmad khan
  • 5,426
  • 1
  • 27
  • 33
7

This error occurs because you are using a higher PHP version and your Laravel application is on an older PHP version.

✅ Simple solution:

Open: app/Providers/AppServiceProvider.php

And in: public function register() { ... } function add following code:

if(version_compare(PHP_VERSION, '7.2.0', '>=')) {
    error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING);
}
Amit Kadam
  • 589
  • 6
  • 7
5

In php 7.2+ count does not work on relation objects, you need to use:

$model->relation()->exists()

Not this (less than php 7.2):

count($model->relation)
Zoe
  • 27,060
  • 21
  • 118
  • 148
paulie3001
  • 51
  • 1
  • 3
  • 1
    `count()` works in PHP 7.2+. His Laravel version is not matching with PHP version in production. It is already answered. https://stackoverflow.com/a/49245096/5958526 – Nazem Mahmud Piash May 16 '20 at 01:51
2

i ran into the same problem (PHP 7.2 + Laravel 5.3) but i don't see any "good" answers here. For me, the problem occurs when i tried to start a Builder from a scope method on the model: SomeModel::forUser() calls scopeForUser(). Trying to build a new Query, it trips on a count($this->wheres) that gets no initial value (null). Because the magic static call to the scope starts the builder, no other conditions have been placed in the object so the property is still null at that point.

i thought it's worth sharing my solution first, then perspective on why i consider it better than Ben's answer. It's not personal, i just disagree.

Solution

i took a cue from this answer about overriding some of the core Illuminate\Database classes...

  1. Extend Illuminate\Database\Eloquent\Model
    Mine is App\Overrides\Database\Eloquent\Model
  2. Extend Illuminate\Database\Eloquent\Builder
    Mine is App\Overrides\Database\Eloquent\Builder
  3. Extend Illuminate\Database\Query\Builder
    Can you guess? App\Overrides\Database\Query\Builder
  4. Tell Laravel to use YOUR Eloquent\Model:
    config/app.php 'aliases' array, replace the 'Eloquent' value
    with your Eloquent\Model FQN

My Model:

namespace App\Overrides\Database\Eloquent;

/*
 * Notes:
 * * Using replacement Query\Builder with ALIAS
 * * Use of Builder in this class is MY Eloquent\Builder
 */
use App\Overrides\Database\Query\Builder as QueryBuilder;
use Illuminate\Database\Eloquent\Model as EloquentModel;

class Model extends EloquentModel
{
    public function newEloquentBuilder($query)
    {
        return new Builder($query);
    }

    protected function newBaseQueryBuilder()
    {
        $conn = $this->getConnection();

        $grammar = $conn->getQueryGrammar();

        return new QueryBuilder($conn, $grammar, $conn->getPostProcessor());
    }
}

My Eloquent\Builder:

namespace App\Overrides\Database\Eloquent;

use Illuminate\Database\Eloquent\Builder as EloquentBuilder;

class Builder extends EloquentBuilder
{
    public function __construct($query)
    {
        parent::__construct($query);

        /*
         * FIX #1: Set properties treated AS arrays
         *         to empty arrays on construct.
         */
        $this->wheres = [];
        // Any other properties treated as arrays should also be initialized.
    }
}

My Query\Builder:

namespace App\Overrides\Database\Query;

use Illuminate\Database\Query\Builder as QueryBuilder;

class Builder extends QueryBuilder
{
    public function __construct()
    {
        parent::__construct(...func_get_args());

        /*
         * FIX #2: Set properties treated AS arrays
         *         to empty arrays on construct.
         */
        $this->wheres = [];
        // Any other properties treated as arrays should also be initialized.
    }
}

This safely preserves the framework's functionality, since the only actual change you're making is initializing properties that should have been in the first place. Everything else will pass instanceof checks used for dynamic loading and dependency injection.

Opinion

While i agree with @ben-harold about every comment he made saying "NEVER edit vendor code," i disagree with the "solution." It's an oversimplification to a much more complex problem.

Upgrade Laravel: to ensure support for PHP 7.2, jumping up several minor versions - if not major releases - is impractical for a lot of teams. As a long term objective, yes of course. As something i can do to get rid of the bug for my deadline? Nope. Upgrading takes a lot of planning and frequently a lot of rewrites as structures, names, and functionality change. It's something to prioritize, but not a need-it-now answer.

Downgrade PHP: same problem. Downgrading into PHP 5.x means A) PHP is EOL, which may be a deal breaker for a lot of customers who have security policies, and B) any usage of PHP 7.x language features have to be scrapped. As with upgrading the framework this is very likely to cause a lot of headaches. It's also an even less useful solution, since walking backward in the language just puts you farther behind and will require more long-term effort.

cautionbug
  • 435
  • 5
  • 18
1

place the below line ob code before the class name in your controllers

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

I was facing the same issue with an external created table (Not using migration or command), After creating the model, I just assigned a table name, but the problem was in my model protected $fillable where I assign string instead of array and error occurred. There is 2 possible solution for that.

  1. Assign an array to your protected $fillable = ['filed1', 'filed2'];
  2. Remove protected $fillable completely (Not Recommended)
class Abc extends Model
{
     protected  $table = 'cities';
     protected $fillable = ['field1','field2', ...];
}
1

Model looking for countable parameter:

class ClassName extend Model {
    protected $fillable=['column_name']; // column in DB of Model is in array
}
סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
0

Before

count($collection['colors'])

Error:Expected type 'Countable|array'. Found 'string'

After

count((array)$collection['colors'])

It works for me!

Hammad
  • 1
  • 1
-1

'vendor\laravel\framework\src\Illuminate\Database\Eloquent\Builder.php' to:

$originalWhereCount = is_array($query->wheres) ? count($query->wheres) : 0;
sshashank124
  • 31,495
  • 9
  • 67
  • 76
-1

I;m using laravel 6.x for this case you can use this way:

 $id = \DB::table('xxxx')->where('id', $id)->count();
Maizied Hasan Majumder
  • 1,197
  • 1
  • 12
  • 25
-3

I Solve this in Laravel 5.6

// in controller

public function index()
{
$todos = Todo::all();
return view('todos.index')->with(['todos' => $todos]);

}

// in view page

@if(count($todos) > 0)
  @foreach($todos as $todo)
    <div class="well">
      <h3>{{$todo->text}}</h3>
      <span class="label label-danger">{{$todo->due}}</span>
    </div>
  @endforeach
@endif