thinkphp6.0 利用模型事件 实现查询数据后根据需求替换某列的内容 如订单状态字符转文字等等

<?php

namespace app\index\model;

use think\Model;

class OrderTable extends Model
{
  protected $table = "order";
  protected $pk = "order_id";

  public static function onAfterRead($order)
  {
    $state = $order->state;//得到状态列内容
    $statestr = '已下单';
      //判断内容,设置实际对应的文字
    switch ($state) {
      case -1:
        $statestr = '已作废';
        break;
      case 1:
        $statestr = '已到仓库';
        break;
      case 2:
        $statestr = '派送中';
        break;
      case 3:
        $statestr = '已完结';
        break;

      default:
        $statestr = '已下单';
        break;
    }
      
    $order->statestr = $statestr;//新增一列  保存替换后的内容
  }
}

猜你喜欢

转载自www.cnblogs.com/tolingsoft/p/12380902.html
今日推荐