去掉Laravel Admin默认的id筛选条件
要去掉Laravel Admin默认的id筛选条件,可以在对应的Model中重写scopeQuery方法。具体步骤如下:
- 打开对应的Model文件,找到
scopeQuery方法; - 复制该方法的代码到Model中,并修改代码以去除id筛选条件。可以使用
removeWhere方法来移除条件,如下所示:
/**
* Get the original query builder used to retrieve the data.
*
* @param bool $excludeDeleted
* @return \Illuminate\Database\Eloquent\Builder
*/
public function newQuery($excludeDeleted = true)
{
$builder = $this->newQueryWithoutScopes();
if ($excludeDeleted && $this->isSoftDeleting()) {
$builder->whereNull($this->getQualifiedDeletedAtColumn());
}
return $builder;
}
/**
* Get a new query builder with no relationships loaded.
*
* @return \Illuminate\Database\Eloquent\Builder
*/
public function newModelQuery()
{
return $this->newQuery();
}
/**
* Get a new query builder that doesn't have any global scopes.
*
* @return \Illuminate\Database\Eloquent\Builder|static
*/
public function newQueryWithoutScopes()
{
return $this->applyScopes($this->newModelQuery());
}
/**
* Apply all of the boot's globally registered scopes.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return \Illuminate\Database\Eloquent\Builder
*/
protected function applyGlobalScopes($builder)
{
foreach ($this->getGlobalScopes() as $identifier => $scope) {
if (! $builder->hasNamedScope($identifier)) {
$builder->withGlobalScope($identifier, $scope);
}
}
return $builder;
}
/**
* Apply the scopes to the Eloquent builder instance and return it.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeWithAll($builder)
{
return $builder->with($this->with)->select($this->table.'.*');
}
/**
* Apply the scopes to the Eloquent builder instance and return it.
*
* @param \Illuminate\Database\Eloquent\Builder $builder
* @return \Illuminate\Database\Eloquent\Builder
*/
public function scopeQuery($builder)
{
$this->applyScopes($builder);
return $builder->orderBy($this->getKeyName(), 'desc');
}
/**
* Remove a where clause from the query.
*
* @param string|array $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return $this
*/
public function removeWhere($column, $operator = null, $value = null, $boolean = 'and')
{
if (is_array($column)) {
foreach ($column as $clause) {
$this->removeWhere(...array_values($clause));
}
} else {
$wheres = $this->getQuery()->wheres;
$this->getQuery()->wheres = collect($wheres)->reject(function ($where) use ($column, $operator, $value, $boolean) {
if ($where['type'] !== 'Basic') {
return false;
}
if ($where['boolean'] !== $boolean) {
return false;
}
if ($where['column'] !== $column) {
return false;
}
if (isset($operator) && $where['operator'] !== $operator) {
return false;
}
if (isset($value) && $where['value'] !== $value) {
return false;
}
return true;
})->values()->all();
}
return $this;
}
- 在
scopeQuery方法中,使用removeWhere方法移除id筛选条件。默认情况下,id筛选条件是where('id', '>', 0),可以使用以下代码来移除它:
public function scopeQuery($builder)
{
$this->applyScopes($builder);
$builder->removeWhere('id', '>', 0);
return $builder->orderBy($this->getKeyName(), 'desc');
}
- 保存文件,刷新Laravel Admin页面,即可看到id筛选条件已被移除。
原文地址: https://www.cveoy.top/t/topic/06U 著作权归作者所有。请勿转载和采集!