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