好的,我在我的 php 项目中使用 Illuminate,最近从 PHP 5 升级到 PHP 7.2。
我的模型中有这个范围
模型.php
public function scopeWhereInOrAny(Builder $query, $field, $value)
{
if ($value === null || $value === '' || count($value) === 0) {
return $query;
}
return $query->whereIn($field, $value);
}
使用 whereIn 时接受 null 的非常简单的范围。现在我在一个函数中使用它
public function getList($params = []) {
$cancelReasons = CancellationReasons
::orderBy('id', 'asc')
->whereInOrAny('type_booking', $params['type_booking']) // HERE
->where('status', '=', 1)
->get(['id','name','type_client']);
return $cancelReasons;
当我调用该方法时,它有点工作,它返回它应该返回的内容,但还在最后添加一个警告,就像这样
[{"id":1,"name":"He cancelado mi viaje","type_client":1},{"id":2,"name":"Quiero hacer otra actividad","type_client":1},{"id":3,"name":"Quiero modificar la reserva","type_client":1},{"id":4,"name":"He encontrado un precio m\u00e1s barato en otra web","type_client":1},{"id":5,"name":"El proveedor me ha pedido que cancele","type_client":1},{"id":6,"name":"Otros motivos","type_client":1},{"id":7,"name":"Condiciones climatol\u00f3gicas","type_client":2},{"id":8,"name":"Problemas en el destino","type_client":2},{"id":9,"name":"No hay un n\u00famero m\u00ednimo de personas","type_client":2},{"id":10,"name":"Falta de disponibilidad","type_client":2},{"id":11,"name":"Restricciones Covid","type_client":2},{"id":12,"name":"Cancelaci\u00f3n solicitada por el cliente","type_client":2},{"id":13,"name":"Otros motivos","type_client":2},{"id":14,"name":"Cese de la colaboraci\u00f3n con el proveedor","type_client":3},{"id":15,"name":"El proveedor no realiz\u00f3 la actividad","type_client":3},{"id":16,"name":"Otros motivos","type_client":3}]<br />
<b>Warning</b>: count(): Parameter must be an array or an object that implements Countable in <b>/var/www/html/newadmin/vendor7/illuminate/database/Eloquent/Builder.php</b> on line <b>1015</b><br />
现在调试我发现问题出在 Builder.php
/**
* Apply the given scope on the current builder instance.
*
* @param callable $scope
* @param array $parameters
* @return mixed
*/
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 = count($query->wheres); // HERE IS THE PROBLEM
$result = $scope(...array_values($parameters)) ?: $this;
if (count($query->wheres) > $originalWhereCount) {
$this->addNewWheresWithinGroup($query, $originalWhereCount);
}
return $result;
}
Aparently $query->wheres 在这种情况下等于 null,而在 php 7.2 中,当在 null 上使用 count 时会引发警告,现在为什么 $query->wheres 等于 null?因为在自定义范围之前没有其他地方所以如果我将地方的顺序更改为
public function getList($params = []) {
$cancelReasons = CancellationReasons
::orderBy('id', 'asc')
->where('status', '=', 1) //This where before the scope
->whereInOrAny('type_booking', $params['type_booking'])
->get(['id','name','type_client']);
return $cancelReasons;
}
它工作得很好!,但我不认为这真的能长期解决问题,肯定它解决了这个特殊情况,但现在每次我想使用一个范围时,我需要先在哪里添加一个随机的?即使我不需要它?这似乎不对。所以我正在寻找一个更干净的解决方案来解决这个问题,它可以与我在查询构建器上使用范围的其他部分一起使用。
我希望有人能帮助我