[求助] 基于 lumen 有一个项目, model 的一个字段为 mysql json 类型,求助如何导出

2020-01-18 15:58:05 +08:00
 fz420

求助

个人经验: 平常开发基于 python、go,完全没学过 php。

最近,有个需求,修改实现一个 lumen 项目的接口,业务逻辑基本实现了。 但是,model 的一个字段在 mysql 中是 json 类型保存,请求接口的返回消息为 json 字符串,需要把这个字段的返回消息生成标准的 json 对象。 google 了半天,没找到方法(个人完全不熟悉 php )。

求教一下大家,如何把下面的 priceinfo 字段在请求接口返回时,生成标准的 json 对象。

文件:Model.php

<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model as BaseModel;


abstract class Model extends BaseModel
{
    public function __construct(array $attributes = []) {
        parent::__construct($attributes);
    }
}

文件:RefundFlowModel.php

<?php

namespace App\Models;

class RefundFlowModel extends Model
{

    protected $table = 't_refund_flow';

    protected $primaryKey = 'id';

    protected $hidden = array();

    protected $fillable = array(
        'instanceId',
        'productId',
        'ownerUin',
        'providerOwnerUin',
        'deliverType',
        'flags',
        'refundType',
        'orderName',
        'refundOrderName',
        'orderType',
        'priceInfo',                // 此字段在 mysql 里为 json 类型
        'status',
        'deleted',
        'isNotify',
        'reasonId',
        'remark',
        'startTime',
        'endTime',
    );
    protected $jsonColumns = ['priceInfo'];

接口返回消息展示

{
    "Response": {
        "RefundOrderSet": [
            {
                "EndTime": "2019-01-23 10:13:09",
                "InstanceId": "1111111111",
                "OrderName": "222222222222",
                "OrderType": "RENEW",
                "OwnerUin": 3333333333,
                "PriceInfo": "{\"spec\": \"规格一\", \"cycle\": \"1 个月\", \"price\": 100, \"specId\": \"97-008592-ybmbkl\", \"cycleId\": null, \"isTrial\"
: false, \"disprice\": 100, \"goodsNum\": 1, \"maxQuota\": 0, \"timeSpan\": 1, \"timeUnit\": \"m\", \"totalCost\": 100, \"trialDays\": 0, \"unitPrice\": 100, \"tradePriceId\": 0, \"realTotalCost\": 100}",
                "RefundOrderName": "",
                "StartTime": "2019-01-23 10:13:09",
                "Status": 2
            }
        ],
        "RequestId": "",
        "TotalCount": 2
    }
}

求教一下大家,如何把上面的 priceinfo 字段在请求接口返回时,生成标准的 json 对象。

1049 次点击
所在节点    问与答
1 条回复
FaceBug
2020-01-18 16:59:59 +08:00
// 查询
$refundInstanceSet = $query->get();
$refundInstanceSet[PriceInfo] = json_decode($refundInstanceSet[PriceInfo] ,true);

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/638965

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX