如何在foreach循环内的laravel blade文件内运行不同的SELECT查询?

问题描述:

我有两个表如何在foreach循环内的laravel blade文件内运行不同的SELECT查询?

women-dress

women-dress_attributes

women-dress具有pricecolorwomen-dress_attributesdisplay_nameattribute_namefilter

filter是一个简单的Y或N值
一个行中women-dress_attributes看起来像

Price price Y 

,因为价格有y的过滤器值我需要从women-dress
我的控制器的部分看起来像这样

$Filter_query = DB::table($attribute_table_name) 
       ->select('display_name','attribute') 
       ->where('filter', '=', 'Y') 
       ->get(); 

和我的刀片文件里面我有

加载不同 price
@foreach($Filter_query as $agm) 
     <h4>{{ $agm->display_name }}</h4> 

@endforeach 

现在我该如何运行另一个sql select distinct查询来加载刀片中的相应值f ILE?

还是有什么更好的做法?

+2

尝试看看https://laravel.com/docs/5.5/queries#joins – Kenneth

+0

这样的事情会为工作你$ Filter_query = DB :: table('women-dress') - > join('women-dress_attributes','women-dress_attributes.women-dress_id','=','women-dress.id') - > distinct('women-dress.price') - > where('women-dress_attributes.filter','=','Y') - > get(); –

在laravel中这样做的正确方法是为每个2表创建一个模型,并在它们之间创建一个关系函数(即dress())。

然后你就可以在你的刀片上调用这个$agm->dress()->price来获得价格。

this用于创建模型

而且​​您的模型之间创建关系