与foreach的集合对象

时间:2017-03-16 17:03:25

标签: laravel

我尝试运行一个查询,显示与Auth :: user() - >结构相关的结构中的许可证

但我只获得了最后一个"结构的许可证。在集合中,我想要来自$ structures集合的所有许可证。

有人有个主意吗?也许我的查询错了..

here my controller : 

    public function licenceToValid(){

            // here i grab the collection of the clubs who are in relation with my "comite " **(Comite have many clubs)** 
            $structures = Structure::where(['structure_pere_id' => Auth::user()->structure->id])->get();


            foreach ($structures as $structure) {

                //here the query to display the licences from the structures 
                $licences = Licencies::where('structure_id' , $structure->id)->get();

            }

            return view('licencie/valider' , compact('licencies'));


        }

1 个答案:

答案 0 :(得分:1)

您可以使用whereIn来解决您的问题并提高效率(1次查询比许多查询更好)。

$structures = Structure::where(['structure_pere_id' => Auth::user()->structure->id])->get();

$licences = Licencies::whereIn('structure_id', $structures->pluck('id'))->get();

return view('licencie/valider' , compact('licencies'));