博客列表 >关于tp6多对多查询隐藏pivot字段

关于tp6多对多查询隐藏pivot字段

过夜茶
过夜茶原创
2021年03月20日 00:00:561602浏览
//    获取文章详情
   public function getPostDetail(){
//       获取所有参数
       $param=request()->param();
        $res= $this->with(['user','images'])
            ->hidden(['images.pivot'])
            ->visible(['user' => ['id','username','userpic'],'images'=>['url']])
            ->find($param['id']);

       return $res;
   }

当查询文章详情时设置了hidden(['images.pivot'])  查询操作手  ,images中的pivot字段无法隐藏   

{
    "msg": "获取成功",
    "data": {
        "detail": {
            "id": 249,
            "user_id": 316,
            "title": "测试",
            "titlepic": "https://ceshi2.dishait.cn//uploads/20191225/6d355dc85db25437b14d72d8d0faed30.jpg",
            "content": "测试",
            "sharenum": 0,
            "path": "未知",
            "type": 0,
            "create_time": "2019-12-25 14:59:38",
            "post_class_id": 6,
            "share_id": 0,
            "isopen": 1,
            "user": {
                "id": 316,
                "username": "18458301505",
                "userpic": null
            },
            "images": [
                {
                    "url": "uploads/20191225/6d355dc85db25437b14d72d8d0faed30.jpg",
                    "pivot": {
                        "id": 264,
                        "post_id": 249,
                        "image_id": 684,
                        "create_time": 1577257178
                    }
                },
                {
                    "url": "uploads/20191225/c842e691361e811c40c947ab3c3c9ef5.jpg",
                    "pivot": {
                        "id": 265,
                        "post_id": 249,
                        "image_id": 685,
                        "create_time": 1577257178
                    }
                },
                {
                    "url": "uploads/20191225/c1b78b5238c58d76b68a3daedd73c917.jpg",
                    "pivot": {
                        "id": 266,
                        "post_id": 249,
                        "image_id": 686,
                        "create_time": 1577257178
                    }
                },
                {
                    "url": "uploads/20191225/c4126a8c382df21c277f4b9ca4667717.jpg",
                    "pivot": {
                        "id": 267,
                        "post_id": 249,
                        "image_id": 687,
                        "create_time": 1577257178
                    }
                },
                {
                    "url": "uploads/20191225/8bf7a423737e02732ab3443e87f3c4b3.jpg",
                    "pivot": {
                        "id": 268,
                        "post_id": 249,
                        "image_id": 688,
                        "create_time": 1577257178
                    }
                },
                {
                    "url": "uploads/20191225/0791fa09ecaa64a31879bbc3c1184932.jpg",
                    "pivot": {
                        "id": 269,
                        "post_id": 249,
                        "image_id": 689,
                        "create_time": 1577257178
                    }
                },
                {
                    "url": "uploads/20191225/ab9c35cbcc4877db9c97fffc67ee5df7.jpg",
                    "pivot": {
                        "id": 270,
                        "post_id": 249,
                        "image_id": 690,
                        "create_time": 1577257178
                    }
                },
                {
                    "url": "uploads/20191225/836197762d5a78d0e2dcca7ea4f7f434.jpg",
                    "pivot": {
                        "id": 271,
                        "post_id": 249,
                        "image_id": 691,
                        "create_time": 1577257178
                    }
                },
                {
                    "url": "uploads/20191225/d272fdaca65290a6986fbd3176e92509.jpg",
                    "pivot": {
                        "id": 272,
                        "post_id": 249,
                        "image_id": 692,
                        "create_time": 1577257178
                    }
                }
            ]
        }
    }
}




处理方法:在images模型中定义 隐藏字段即可

 * @mixin \think\Model
 */
class Image extends Model
{
    protected $hidden=['pivot'];
   ...
 }



声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议