Laravel渴望不与选择功能一起使用

时间:2020-07-26 13:26:14

标签: laravel

如果我使用选择功能,数据将消失

$regions = Region::select('id', 'place_id', 'formatted_address')
        ->where('lang', $lang)
        ->with(['areas' => function($query) {
            return $query
                ->select('id', 'place_id', 'formatted_address') // << the problem is here
                ->whereNotNull('area1')->whereNull('locality')->whereNull('area2');
        }])
        ->get();

enter image description here

如果删除选择功能,则区域字段将填充数据

关系:

$this->hasMany(Region::class, 'country', 'formatted_address');

1 个答案:

答案 0 :(得分:0)

请求中必须包含参与关系的字段

$this->hasMany(Region::class, 'country', 'formatted_address');

->select('place_id', 'country', 'formatted_address')