CaiWuXiTong/app/Traits/Scopes.php
15061959991 1adf627c0d feat: 财务系统二次开发 - 营销账户账单功能
- 基础环境:Docker 部署配置,源码挂载实时同步
- 修复:super_admin 权限补全、登录重定向循环
- 开发:店铺营销账户账单 - 抖音账户(15字段汇总账单)
- 开发:店铺营销账户账单 - 淘系支付宝账户(8字段流水账单)
- 开发:店铺营销账户账单 - 拼多多账户(7字段流水账单)
- 业务页面导航集成:config/business_navigation.php + Business\Pages 控制器
- 各平台独立表结构和视图,互不影响
2026-07-21 18:43:52 +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);
}
}