用于切换全局范围的后端列表过滤器
Backend list filter to toggle global scope
我有一个职位空缺模型,它在全球范围内只显示空缺职位,但在后端我想要一个复选框来切换它,以便可以显示所有职位空缺。复选框类型似乎只适用于本地范围。有人可以帮忙吗?
您可以在删除全局范围的模型上设置本地范围。
类似于:
public function scopeAllVacancies($query)
{
// This is not actually how you remove a global scope,
// see https://softonsofa.com/laravel-5-eloquent-global-scope-how-to/
// for more information.
$query->removeScope(MyGlobalScope);
}
附带说明一下,您将无法使用 https://softonsofa.com/laravel-5-eloquent-global-scope-how-to/ 中建议的删除方法,因为列表小部件正在通过引用使用该对象,并且不会收听 return 这个范围的价值。因此,为了当前删除它(至少在我们实现某种形式的 $query->resetQuery()
或 $query->removeScope($scope)
之前),您必须在查询对象上手动执行范围的删除。
下面是一个直接与查询对象交互以手动删除范围的示例:
/**
* Removes the provided orderby column rule from the QueryBuilder orders array to reset the orderBy for that column
*
* @param Builder $query
* @param string $orderByColumn
* @return Builder $query
*/
public function scopeRemoveOrder($query, $orderByColumn)
{
// Orders property can have mixed up integer index, flatten the array to negate any weird keys in this array
$orders = $query->getQuery()->orders;
if (is_array($orders)) {
$orders = array_values($query->getQuery()->orders);
$i = 0;
foreach ($orders as $order) {
if ($order['column'] === $orderByColumn) {
unset($orders[$i]);
}
$i++;
}
$query->getQuery()->orders = $orders;
}
return $query;
}
我有一个职位空缺模型,它在全球范围内只显示空缺职位,但在后端我想要一个复选框来切换它,以便可以显示所有职位空缺。复选框类型似乎只适用于本地范围。有人可以帮忙吗?
您可以在删除全局范围的模型上设置本地范围。
类似于:
public function scopeAllVacancies($query)
{
// This is not actually how you remove a global scope,
// see https://softonsofa.com/laravel-5-eloquent-global-scope-how-to/
// for more information.
$query->removeScope(MyGlobalScope);
}
附带说明一下,您将无法使用 https://softonsofa.com/laravel-5-eloquent-global-scope-how-to/ 中建议的删除方法,因为列表小部件正在通过引用使用该对象,并且不会收听 return 这个范围的价值。因此,为了当前删除它(至少在我们实现某种形式的 $query->resetQuery()
或 $query->removeScope($scope)
之前),您必须在查询对象上手动执行范围的删除。
下面是一个直接与查询对象交互以手动删除范围的示例:
/**
* Removes the provided orderby column rule from the QueryBuilder orders array to reset the orderBy for that column
*
* @param Builder $query
* @param string $orderByColumn
* @return Builder $query
*/
public function scopeRemoveOrder($query, $orderByColumn)
{
// Orders property can have mixed up integer index, flatten the array to negate any weird keys in this array
$orders = $query->getQuery()->orders;
if (is_array($orders)) {
$orders = array_values($query->getQuery()->orders);
$i = 0;
foreach ($orders as $order) {
if ($order['column'] === $orderByColumn) {
unset($orders[$i]);
}
$i++;
}
$query->getQuery()->orders = $orders;
}
return $query;
}