简体   繁体   English

关系问题有很多 laravel 属性复古

[英]problem with relation hasmany laravel proprty retro

I have a relation hasMany but it doesn't work at my controller我有一个关系 hasMany 但它在我的 controller 上不起作用

code season model:代码季 model:

public function retro () {
    return $this->hasMany('App\Models\Retrocession','season_id');
}

code controller:代码 controller:

$hotelagencie = HotelAgency::find($id) ;
    $hotel = $hotelagencie->hotel;
    $season = $hotel->seasons;
    return  $season->retro

Ereur: Property [retro] does not exist on this collection instance. Ereur:此集合实例上不存在属性 [retro]。

Thxyou.谢谢。

We confirmed what you want(Select all the retro from all the seasons), so I think I can give you an answer:我们确认了你想要什么(从所有季节中选择所有复古),所以我想我可以给你一个答案:

This is what you got:这就是你得到的:

$hotelagencie = HotelAgency::find($id) ;
    $hotel = $hotelagencie->hotel;
    $season = $hotel->seasons;
    return  $season->retro

This is what I think it needs to be:这就是我认为它需要的:

$hotelagencie = HotelAgency::find($id) ;
    $hotel = $hotelagencie->hotel;
    $seasons = $hotel->seasons;
    return $seasons->pluck('retro');

For more info on what pluck() is and how to use it, please refer to the official docs:有关pluck()是什么以及如何使用它的更多信息,请参阅官方文档:

https://laravel.com/docs/6.x/collections#method-pluck https://laravel.com/docs/6.x/collections#method-pluck

I hope it helps;)我希望它有所帮助;)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM