Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

TrentDPT's avatar

Homestead - count(): Parameter must be an array....

Hey everyone,

I seem to have no issue when i run a server through xampp & use myphpadmin, but then when i switched the project over to homestead & used $faker to create the entire DB i get a count() error.

Wheb i use php tinker im able to see that all the proper data is input, but i still get this odd error. Its the equivalent setup to the Forums TDD series, and the threads. Where he uses the scope & builder.

(1/1) ErrorException
count(): Parameter must be an array or an object that implements Countable
in Builder.php (line 932)
at HandleExceptions->handleError(2, 'count(): Parameter must be an array or an object that implements Countable', '/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php', 932, array('scope' => array(object(Reference), 'scopeForRegion'), 'parameters' => array(object(Builder), object(Region)), 'query' => object(Builder), 'originalWhereCount' => 0, 'result' => object(Builder)))
at count(null)
in Builder.php (line 932)
at Builder->callScope(array(object(Reference), 'scopeForRegion'), array(object(Builder), object(Region)))
in Builder.php (line 1243)
at Builder->__call('forRegion', array(object(Region)))
in Model.php (line 1357)
at Model->__call('forRegion', array(object(Region)))
in Model.php (line 1369)
at Model::__callStatic('forRegion', array(object(Region)))
in ReferenceController.php (line 24)
at ReferenceController->index(object(Region), object(Type))
at call_user_func_array(array(object(ReferenceController), 'index'), array(object(Region), object(Type)))
in Controller.php (line 55)
at Controller->callAction('index', array(object(Region), object(Type)))
in ControllerDispatcher.php (line 44)

In Builder.php

protected function callScope(callable $scope, $parameters = [])
    {
        array_unshift($parameters, $this);

        $query = $this->getQuery();

        // We will keep track of how many wheres are on the query before running the
        // scope so that we can properly group the added scope constraints in the
        // query as their own isolated nested where statement and avoid issues.
        $originalWhereCount = is_null($query->wheres)
                    ? 0 : count($query->wheres);

        $result = $scope(...array_values($parameters)) ?? $this;

        if (count((array) $query->wheres) > $originalWhereCount) {
            $this->addNewWheresWithinGroup($query, $originalWhereCount);
        }

        return $result;
    }

Controller

public function index(Region $region = null, Type $type)
    {
        $references = Reference::forRegion($region)
            ->orderBy('name', 'asc')
            ->paginate(20);

        $regions = Region::orderBy('title', 'asc')->get();
        $types = Type::get();
        return view('reference.index', compact('references', 'regions', 'region', 'types'));
    }

Model

public function scopeForRegion($builder, $region)
    {
        if($region->id){
            return $builder->where('region_id', $region->id);
        }

        return $builder;

    }
0 likes
15 replies
junlue's avatar

It's a problem with PHP 7.2. You will have to downgrade your homestead version to 6.6 and reinstall your homestead box

  1. vagrant box remove [boxid]. To check the [boxid]run vagrant box list
  2. vagrant destroy [imageid]. To check your [imageid]run vagrant status
  3. vagrant up

Please note to backup your database and files as this process will remove your data.

1 like
TrentDPT's avatar

Well thats a bummer. No solutions in the works currently? I take it this is a fairly new issue?

Ok, ill try and walk it back then. Appreciate it. Ill let you know how it goes

TrentDPT's avatar

@junlue, I have another quick question for you. What you told me to do worked perfectly, but not when I run phpunit tests I get the same error in my test environments....

ErrorException: Count(): Parameter must be an array or an object that implements Countable

There has to be something simple that I'm just missing here...thoughts? Is it that I need an older version of phpunit now installed?

shez1983's avatar

you didnt have to downgrade your homestead.. in your homestead.yml file add

php: 7.1

sites:
    - map: homestead.test
      to: /home/vagrant/code/Laravel/public
      php: "7.1"

;)

2 likes
TrentDPT's avatar

@shez1983 If i've already taken the steps as above, do I just add that line you listed? or do I need to "rerun" or do something else to implement it?

lostdreamer_nl's avatar

you probably still have php 7.2 installed and running on the command line. try this command on the command line:

php -v

to see which version you're running, if 7.2 ->downgrade

ejdelmonico's avatar

@TrentDPT Go ahead and destroy the vagrant box and add the php version line to your Homestead.yaml. Run vagrant up and you should be ok after the box rebuilds itself. If not, shoot me an email.

1 like
TrentDPT's avatar

@ejdelmonico , @shez1983

So i added

sites:
    -
        map: brooksihl.dev
        to: /home/vagrant/brooksihl/public
        php: "7.1"

and then i ran vagrant destroy, followed by vagrant up. When I go back in the website itself has no issues with count(), but when i run vendor/bin/phpunit there is still the count() error. Does this have to do with this...

"require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7"

?

shez1983's avatar

aah you didnt have to destroy.. just vagrant reload or vagrant reload --provision... not sure why people give 'bad' advice (well not bad but i KNOW destroying and redoing takes AGES)

check what version you have now.. but i do not think phpunit is a problem.. can u tell me the exact error and where it occurs? is that YOUR code or built-in? if built in try going back to a previous version of phpunit just in case..

TrentDPT's avatar

@shez1983

I added the above, and it works in the browser, but I still get this

vagrant@brooksihl:~/brooksihl$ php -v
PHP 7.2.0-1+ubuntu16.04.1+deb.sury.org+1 (cli) (built: Nov 30 2017 13:58:33) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2017 Zend Technologies

and this is the error (only on tests)

1) Tests\Feature\ReadPtCaseTest::test_a_user_can_browse_cases
ErrorException: count(): Parameter must be an array or an object that implements Countable

/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:932
/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Builder.php:1243
/home/vagrant/brooksihl/app/Http/Controllers/PtCaseController.php:142
/home/vagrant/brooksihl/app/Http/Controllers/PtCaseController.php:27
/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Routing/Controller.php:55
/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Routing/ControllerDispatcher.php:44
/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Routing/Route.php:203
/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Routing/Route.php:160
/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Routing/Router.php:574
/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php:30
/home/vagrant/brooksihl/vendor/laravel/framework/src/Illuminate/Routing/Middleware/SubstituteBindings.php:41
ejdelmonico's avatar

@TrentDPT phpunit 5.7 still works with php 7.1. That is odd that you have 5.7 though because your project (assuming 5.5) should install 6.0. Have you run composer global update on your machine lately? If composer has 5.7 cached and laravel 5.5 will take either 6.0 or 5.7, then you will end up with 5.7. So, my guess is the count error is because you have php 7.2 installed on your computer.

shez1983's avatar
shez1983
Best Answer
Level 23

thats because php has two distinct sevices.. one is phpfpm which browsers use (to paraphrase it) & the other you use on the cli.. php-cli.. i think that might still be using php 7.2 so you will have to downgrade that. (again search on google)

dinni's avatar

place this in web.php it will do the job for you

if(version_compare(PHP_VERSION, '7.2.0', '>=')) { error_reporting(E_ALL ^ E_NOTICE ^ E_WARNING); }

Please or to participate in this conversation.