修复「是否种菜订单」映射读取为空:补 deleted_at 列并改用表查询

根因:映射表模型继承的基类带 SoftDeletes,而新建的 jst_order_label_planting_rules 没有 deleted_at 列,
查询直接报错(被服务兜底成空映射),页面全部显示「否」。
- 新增迁移 2026_10_07_130000 补 deleted_at(建表迁移也同步补上,供全新安装)
- 服务改用 DB 表查询,避开基类软删除/公司作用域与模型缓存,导入后立即失效重读
This commit is contained in:
qiuxiang 2026-10-07 11:19:53 +08:00
parent d877735507
commit b53555b74c
3 changed files with 41 additions and 1 deletions

View File

@ -3,6 +3,7 @@
namespace App\Business;
use App\Models\Business\JstOrderLabelPlantingRule;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Schema;
use Throwable;
@ -26,8 +27,9 @@ class JstOrderLabelPlantingRules
return [];
}
// 直接用表查询:避开基类的软删除/公司作用域与模型缓存,映射表由命令写入后立即失效重读
$map = [];
foreach (JstOrderLabelPlantingRule::query()
foreach (DB::table('jst_order_label_planting_rules')
->where('company_id', $companyId)
->get(['label', 'is_planting']) as $row) {
$label = trim((string) $row->label);

View File

@ -20,6 +20,7 @@ return new class extends Migration
$table->string('label', 191)->comment('聚水潭订单标签');
$table->string('is_planting', 8)->default('是')->comment('是否种菜订单:是/否');
$table->timestamps();
$table->softDeletes();
$table->unique(['company_id', 'label'], 'jst_order_label_planting_unique');
});

View File

@ -0,0 +1,37 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
/**
* 补 jst_order_label_planting_rules.deleted_at:
* 模型继承的基类带 SoftDeletes,缺这一列会让查询直接报错(映射读成空)。
* 2026_10_07_120000 的建表语句已同步补上,这条用于已经建过表的库。
*/
return new class extends Migration
{
public function up(): void
{
if (! Schema::hasTable('jst_order_label_planting_rules')
|| Schema::hasColumn('jst_order_label_planting_rules', 'deleted_at')) {
return;
}
Schema::table('jst_order_label_planting_rules', function (Blueprint $table): void {
$table->softDeletes();
});
}
public function down(): void
{
if (! Schema::hasTable('jst_order_label_planting_rules')
|| ! Schema::hasColumn('jst_order_label_planting_rules', 'deleted_at')) {
return;
}
Schema::table('jst_order_label_planting_rules', function (Blueprint $table): void {
$table->dropSoftDeletes();
});
}
};