laravel - 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();
如果删除选择功能,则区域字段将填充数据
关系:
$this->hasMany(Region::class, 'country', 'formatted_address');
解决方案
请求必须包含参与关系的字段
$this->hasMany(Region::class, 'country', 'formatted_address');
->select('place_id', 'country', 'formatted_address')