我有一个具有hasOne关系的模型,
public function geoTournee()
{
return $this->hasOne(GeoTournee::class, 'id_tournee','id_tournee');
}
我已经加载了该关系,并希望从集合中删除一个项目。
这是之后的结果,我只想获取行程内容,然后删除geo_tounee和其他字段。
"geo_tournee": {
"id_geo_tournee": 46,
"itineraire": [
{
"lat": 8000,
"lon": 3000,
"date": "2020:04:21 16:58:23"
},
{
"lat": 8000,
"lon": 3000,
"date": "2020:04:21 16:58:23"
},
{
"lat": 8000,
"lon": 3000,
"date": "2020:04:21 16:58:23"
},
{
"lat": 5000,
"lon": 9000,
"date": "2020:04:21 16:58:23"
},
{
"lat": 5000,
"lon": 9000,
"date": "2020:04:21 16:58:23"
},
{
"lat": 5000,
"lon": 9000,
"date": "2020:04:21 16:58:23"
}
],
"id_tournee": 33,
"created_at": "2020-04-23 18:10:32",
"updated_at": "2020-04-23 22:26:53"
任何想法做到这一点!
答案 0 :(得分:1)
您需要执行以下操作:
return $this->hasOne(GeoTournee::class, 'id_tournee','id_tournee')->select(['id_tournee', 'itineraire']);
您也可以尝试快速加载:
$user = App\User::with(['geoTournee' => function ($query) {
$query->select('itineraire');
}])->get();
或以下
App\User::with('geoTournee:itineraire')->get();