Files
Xboard/app/Traits/QueryOperators.php

68 lines
1.7 KiB
PHP
Raw Permalink Normal View History

<?php
namespace App\Traits;
Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.
2025-07-05 12:35:12 +08:00
use Illuminate\Contracts\Database\Query\Expression;
trait QueryOperators
{
/**
* 获取查询运算符映射
*
* @param string $operator
* @return string
*/
protected function getQueryOperator(string $operator): string
{
return match (strtolower($operator)) {
'eq' => '=',
'gt' => '>',
'gte' => '>=',
'lt' => '<',
'lte' => '<=',
'like' => 'like',
'notlike' => 'not like',
'null' => 'null',
'notnull' => 'notnull',
default => 'like'
};
}
/**
* 获取查询值格式化
*
* @param string $operator
* @param mixed $value
* @return mixed
*/
protected function formatQueryValue(string $operator, mixed $value): mixed
{
return match (strtolower($operator)) {
'like', 'notlike' => "%{$value}%",
'null', 'notnull' => null,
default => $value
};
}
/**
* 应用查询条件
*
* @param \Illuminate\Database\Eloquent\Builder $query
* @param string $field
* @param string $operator
* @param mixed $value
* @return void
*/
Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.Fix: Handle raw DB expressions in user filter A TypeError occurred when filtering the user list by the `total_used` computed attribute. The `applyQueryCondition` method expected a string for the field name, but received an `Illuminate\Database\Query\Expression` object from `DB::raw`. This commit updates the `buildFilterQuery` method to check if the query field is an `Expression` instance. If so, it now uses `whereRaw` to apply the filter, preventing the type error and enabling filtering on computed fields. For standard string fields, it continues to use the original `applyQueryCondition` logic.
2025-07-05 12:35:12 +08:00
protected function applyQueryCondition($query, array|Expression|string $field, string $operator, mixed $value): void
{
$queryOperator = $this->getQueryOperator($operator);
if ($queryOperator === 'null') {
$query->whereNull($field);
} elseif ($queryOperator === 'notnull') {
$query->whereNotNull($field);
} else {
$query->where($field, $queryOperator, $this->formatQueryValue($operator, $value));
}
}
}