php Laravel我怎么能查询关系上的急切加载,只是显示查询的项目

rggaifut  于 2022-12-10  发布在  PHP
关注(0)|答案(1)|浏览(103)

查询关系时遇到一些问题,我想查询关系中的数据,但只显示具有关系数据的属性项。

$property = Property::with([
        'member' => function ($query) use ($value) {
           return $query->select('ID', 'NAMA')->where('NAMA', $value);
    },
        'province:ID,NAMA',
        'city:ID,NAMA',
        'district:ID,NAMA',
        'village:ID,NAMA',
        'unitcertificate',
        'facility.facility',
        'furniture.furniture',
        'accessibility.accessibility',
        'nearest_area',
        'file'])->take(5)->get();

public function member()
{
    return $this->hasOne(Member::class, 'ID', 'id_agen');
}

例如,我有一个表property,但我正在查询member的关系表,我希望只查询关系上的数据,而不是所有属性。

{
      "id_property": "PR10130420221200002",
      "property_name": "Elroy Erasmo Flower",
      "property_price": 1000000000000,
      "subsidi_type": 2,
      "property_type": "2",
      "unit_type": 3,
      "id_agen": 1,
      "postal_code": 53177,
      "sale_status_id": 1,
      "latitude": -6.97003693,
      "longitude": 110.46976524,
      "created_at": "2022-12-04T07:24:41.000000Z",
      "updated_at": "2022-12-04T07:24:41.000000Z",
      "member": null,
    },
    {
        "id_property": "PR10130420221200004",
        "property_name": "Becki Nancie CV",
        "property_price": 2000000000000,
        "subsidi_type": 1,
        "property_type": "2",
        "unit_type": 2,
        "url_video": "https://www.youtube.com/watch?v=GYIuPDJK7oc",
        "id_agen": 55,
        "postal_code": 53177,
        "sale_status_id": 1,
        "latitude": -6.48328224,
        "longitude": 106.98369993,
        "created_at": "2022-12-04T07:24:41.000000Z",
        "updated_at": "2022-12-04T07:24:41.000000Z",
        "member": {
            "ID": 55,
            "NAMA": "Sella Augusta Bulan"
        },
    },
    {
        "id_property": "PR10130420221200004",
        "property_name": "Becki Nancie CV",
        "property_price": 2000000000000,
        "subsidi_type": 1,
        "property_type": "2",
        "unit_type": 2,,
        "id_agen": 55,
      "postal_code": 53177,
        "sale_status_id": 1,
        "latitude": -6.48328224,
        "longitude": 106.98369993,
        "created_at": "2022-12-04T07:24:41.000000Z",
        "updated_at": "2022-12-04T07:24:41.000000Z",
        "member": {
            "ID": 55,
            "NAMA": "Sella Augusta Bulan"
        },
    }

我仍然可以得到没有查询结果的成员的属性数据,这意味着成员:null,我希望我能得到这样的

{
        "id_property": "PR10130420221200004",
        "property_name": "Becki Nancie CV",
        "property_price": 2000000000000,
        "subsidi_type": 1,
        "property_type": "2",
        "unit_type": 2,
        "url_video": "https://www.youtube.com/watch?v=GYIuPDJK7oc",
        "id_agen": 55,
        "postal_code": 53177,
        "sale_status_id": 1,
        "latitude": -6.48328224,
        "longitude": 106.98369993,
        "created_at": "2022-12-04T07:24:41.000000Z",
        "updated_at": "2022-12-04T07:24:41.000000Z",
        "member": {
            "ID": 55,
            "NAMA": "Sella Augusta Bulan"
        },
    },
    {
        "id_property": "PR10130420221200004",
        "property_name": "Becki Nancie CV",
        "property_price": 2000000000000,
        "subsidi_type": 1,
        "property_type": "2",
        "unit_type": 2,,
        "id_agen": 55,
        "postal_code": 53177,
        "sale_status_id": 1,
        "latitude": -6.48328224,
        "longitude": 106.98369993,
        "created_at": "2022-12-04T07:24:41.000000Z",
        "updated_at": "2022-12-04T07:24:41.000000Z",
        "member": {
            "ID": 55,
            "NAMA": "Sella Augusta Bulan"
        },
    }

问题是表成员在另一个数据库上
我该怎么解决这个问题?

njthzxwz

njthzxwz1#

尝试位置

$property = Property::whereHas('member', function ($query) use ($value) {
    return $query->select('ID', 'NAMA')->where('NAMA', $value);
} 
...
...

相关问题