CaiWuXiTong/app/Traits/Scopes.php
WuYongJie 9595267ee7
Some checks failed
Tests / PHP 8.1 (push) Has been cancelled
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
chore: 导入 Akaunting 初始源码与团队协作规则
2026-07-20 18:20:25 +08:00

115 lines
3.0 KiB
PHP

<?php
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;
trait Scopes
{
public function applyNotRecurringScope(Builder $builder, Model $model): void
{
// Skip if type already set
if ($this->scopeColumnExists($builder, $model->getTable(), 'type')) {
return;
}
// Apply not recurring scope
$builder->isNotRecurring();
}
public function applyNotSplitScope(Builder $builder, Model $model): void
{
// Skip if type already set
if ($this->scopeColumnExists($builder, $model->getTable(), 'type')) {
return;
}
// Apply not split scope
$builder->isNotSplit();
}
public function scopeColumnExists(Builder $builder, string $table, string $column): bool
{
$query = $builder->getQuery();
foreach ((array) $query->wheres as $key => $where) {
if (empty($where) || empty($where['column'])) {
continue;
}
// Skip Expression objects (e.g. DB::raw()) — they are not
// simple "table.column" strings and cannot be parsed here.
// Fixes CLOUD-PROD-LARAVEL-1TKA
if (! is_string($where['column'])) {
continue;
}
if (strstr($where['column'], '.')) {
$whr = explode('.', $where['column']);
$where['table'] = $whr[0];
$where['column'] = $whr[1];
}
if (! empty($where['table']) && ! empty($table) && ($where['table'] != $table)) {
continue;
}
if ($where['column'] != $column) {
continue;
}
return true;
}
return false;
}
public function scopeValueExists(Builder $builder, string $table, string $column, string $value): bool
{
$query = $builder->getQuery();
foreach ((array) $query->wheres as $key => $where) {
if (empty($where) || empty($where['column']) || empty($where['value'])) {
continue;
}
// Skip Expression objects — same guard as scopeColumnExists
if (! is_string($where['column'])) {
continue;
}
if (strstr($where['column'], '.')) {
$whr = explode('.', $where['column']);
$where['table'] = $whr[0];
$where['column'] = $whr[1];
}
if (! empty($where['table']) && ! empty($table) && ($where['table'] != $table)) {
continue;
}
if ($where['column'] != $column) {
continue;
}
if (! Str::endsWith($where['value'], $value)) {
continue;
}
return true;
}
return false;
}
// @deprecated version 3.0.0
public function scopeExists($builder, $column): bool
{
return $this->scopeColumnExists($builder, '', $column);
}
}