美文网首页
TP5.1模型关联,查询被关联模型中包括软删除的数据

TP5.1模型关联,查询被关联模型中包括软删除的数据

作者: 吴涛涛 | 来源:发表于2020-06-17 17:00 被阅读0次

1.模型代码

<?php

namespace app\admin\model;

use think\Model;

class OrderModel extends Model
{
    //表名
    protected $name = 'order';
    //主键
    protected $pk = 'id';

    //管理商品模型
    public function goods()
    {
        return $this->belongsTo('GoodsModel', 'goods_id', 'id')->removeOption('soft_delete');
    }
}

2.控制器代码

public function index()
    {
        //显示后台订单列表
        $data = OrderModel::with('goods')->all();
        $data->visible(['goods'=>['name','goods_detail']])->toArray();
        //判断
        if ($data) {
            return \sendResponse($data);
        }

        return \sendResponse([]);
    }

相关文章

网友评论

      本文标题:TP5.1模型关联,查询被关联模型中包括软删除的数据

      本文链接:https://www.haomeiwen.com/subject/btbsxktx.html