Nixus

3621 经验值

{
        "current_page": 2,
        "data": [
            {
                "store_group_id": 2,
                "channel": {
                    "id": 2,
                    "channel_name": "石家庄金平区"
                }
            },
            {
                "store_group_id": 2,
                "channel": {
                    "id": 2,
                    "channel_name": "石家庄金平区"
                }
            }
        ],
        "first_page_url": "http://pinkr.work/Crm/Store/GetStoresByChannel?page=1",
        "from": 3,
        "last_page": 3,
        "last_page_url": "http://pinkr.work/Crm/Store/GetStoresByChannel?page=3",
        "next_page_url": "http://pinkr.work/Crm/Store/GetStoresByChannel?page=3",
        "path": "http://pinkr.work/Crm/Store/GetStoresByChannel",
        "per_page": "2",
        "prev_page_url": "http://pinkr.work/Crm/Store/GetStoresByChannel?page=1",
        "to": 4,
        "total": 5
    }

这里面中的first_page_urllast_page_urlnext_page_urlprev_page_urlpath 并不需要,怎样把这些字段过滤掉?

$stores = Store::where($where)
    ->with([
        'channel' => function ($query) {
            $query->select('id', 'channel_name'); 
        },
    ])
    ->select('store_group_id') 
    ->paginate($page_size)
    ->toArray();

这个查询中,with中的数据,会作为store中的一个字段,值为array的形式返回
怎样使这个数组合并到父表中的数据呢?

擦……我知道了,关联的字段,必须在select中才会有数据,如果没有关联字段,就返回null

$stores = Store::where($where)
    ->with([
        'channel' => function ($query) {
            $query->select('id', 'channel_name'); // 父表中,`id`关联到了子表中的`store_group_id` 所以,select中必须要有`id`,才会有数据
        },
    ])
    ->select('store_group_id') // 子表中的外键是 `store_group_id` 必须要有这个,父表才有数据
    ->paginate($page_size)
    ->toArray();

数据库:

store表:
id,store_group_id, ...

channel表:
id, channel_name, ...

模型代码:

Store.php Store模型:
public function channel()
{
    return $this->belongsTo(StoreChannel::class, 'store_group_id');
}

控制器代码:

$stores = Store::where($where)
    ->with('channel')
    ->select('store_name')  // 当加上这个的时候,with('channel')返回的就是null
    ->paginate($page_size)
    ->toArray();

结果:
加了->select('store_name')

        "data": [
            {
                "store_name": "suscipit",
                "channel": null
            },
            {
                "store_name": "at",
                "channel": null
            }
        ]

不加->select('store_name')

        "data": [
            {
                "id": 16,
                "business_no": "111111",
                "store_no": "1903",
                "brand": "sit",
                "store_code": "1740",
                "store_name": "suscipit",
                "store_group_id": 2,
                "channel": {
                    "id": 2,
                    "channel_name": "石家庄金平区",
                }
            },
            {
                "id": 25,
                "business_no": "111111",
                "store_no": "4591",
                "brand": "voluptatum",
                "store_code": "4974",
                "store_name": "at",
                "store_group_id": 2,
                "channel": {
                    "id": 2,
                    "channel_name": "石家庄金平区",
                }
            }
        ]

不行的,如果是belongsTo的话,拿到的是null
paginate返回的分页数据中,可以过滤掉一些不需要的吗?
比如first_page_url,在API这种完全不需要,有办法删掉吗?
搜一下,全是自定义分页模板的

老师,这个方法对hasMany有效,对belongTo无效,对吧?我现在是belongTo的联表查询需要,有没有办法实现?

有区别的,完全可以解决掉创建多个API Resource的问题

老师,laravel中有验证数值大小的吗?
我看了一下文档,找到了一个between,设置了between:1,100
结果,在值为0,-1的时候,依然通过了验证