Laravel 报错 count(): Parameter must be an array

Spoony 54.4m2023-07-29300 次点击
PHP 版本从 7.0 升级到 7.4,出现多处错误。

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

count 函数在 PHP 7.2 中已经是严格要求传入的参数类型了,只能是数组或者 Countable 对象。而在 laraval 5.3 中

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

$query = $this->getQuery();

//问题出现在这里 $query->wheres 结果为 null 导致count报错
$originalWhereCount = count($query->wheres);

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

if ($this->shouldNestWheresForScope($query, $originalWhereCount)) {‌
$this->nestWheresForScope($query, $originalWhereCount);
}

return $result;
}
```

## 解决方案
1、降低 php 版本至于 7.2 以下
2、保证至少有一个条件 比如 $query->where('deleted_at','==','null')
3、orderby()、count()、limit() 这三个方法不能直接在 model:: 后面用,也需要先 where 一下。
收藏 ♥ 感谢
Spoony 小组长 2023-08-03 
另外发现异常:Unparenthesized `a ? b : c ? d : e` is deprecated. Use either `(a ? b : c) ? d : e` or `a ? b : (c...

$remark = $chargeType == 2 ? 'VIP题库' : $type == 4 ? '历史题库' : '章节题库';

php 7.4 开始也理解不了。需要加上括号。如下。

$remark = ($chargeType == 2 ? 'VIP题库' : $type == 4) ? '历史题库' : '章节题库';

登录注册 后可回复。



GitHub