猿问

Laravel中三个表的复杂查询

我有一个Laravel无法解决的问题。我有三个表:


用户,属性,优惠


用户具有许多属性和要约属性和要约属于用户


Properties table:

id

user_id

contract_start_date

other_date

created_at

updated_at


Offers table:

id

user_id

created_at

updated_at

我想给一个这样的表(我使用两个日期过滤器:startDate和endDate):


Users name || Properties count (They are filtered by contract_start_date)  || Properties count (They are filtered by other_date) || Offers count

———


user1 || 5 || 2 || 12

user2 || 0 || 1 || 0

user3 || 0 || 0 || 0

我尝试使用union,leftJoin等,但是我无法解决此问题……谢谢,如果可以的话


呼如林
浏览 216回答 1
1回答

Qyouu

快速与肮脏首先,让您的用户渴望加载其属性的日期和所有报价。$users = Users::with([&nbsp; &nbsp; 'property'=>function($query){&nbsp; &nbsp; &nbsp; &nbsp; $query->select('contract_startdate','otherdate');&nbsp; &nbsp; },&nbsp; &nbsp; 'offer'=>function($query){&nbsp; &nbsp; &nbsp; &nbsp; $query->select('id');&nbsp; &nbsp; },);假设您已$dates在模型中正确设置数组以包含contract_startdate和other_date。我们可以使用碳来过滤集合,以获得我们感兴趣的属性。在您看来,您可以:<table>&nbsp; <thead>&nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp;<th>User</th>&nbsp; &nbsp; &nbsp; &nbsp;<th>Property (start date)</th>&nbsp; &nbsp; &nbsp; &nbsp;<th>Property (other date)</th>&nbsp; &nbsp; &nbsp; &nbsp;<th>Offers</th>&nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp;</thead>&nbsp; &nbsp;<tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;@foreach($users as $user)&nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{$user->name}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{$user->properties&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->filter(function($item) use ($filter_start,$filter_end){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return $item->contract_startdate->between($filter_start,$filter_end);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})->count() }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{$user->properties&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;->filter(function($item) use ($filter_start,$filter_end){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;return return $item->other_date->between($filter_start,$filter_end);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;})->count() }}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{$user->offers->count()}}</td>&nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp;@endforeach&nbsp; &nbsp;</tbody></table>清理您可能应该将过滤器从视图中重构出来以保持整洁,但是这样做会在集合上添加另一个循环。但是您可以通过在控制器中执行类似的操作来删除循环。$users = Users::with([&nbsp; &nbsp; 'property'=>function($query){&nbsp; &nbsp; &nbsp; &nbsp; $query->select('contract_startdate','otherdate');&nbsp; &nbsp; },&nbsp; &nbsp; 'offer'=>function($query){&nbsp; &nbsp; &nbsp; &nbsp; $query->select('id');&nbsp; &nbsp; },);$byContractDate = collect();$byOtherDate = collect();foreach($users as $user){&nbsp; &nbsp; foreach($properties as $property){&nbsp; &nbsp; &nbsp; &nbsp; $contractCounter = 0;&nbsp; &nbsp; &nbsp; &nbsp; $otherCounter = 0;&nbsp; &nbsp; &nbsp; &nbsp; if($propery->contract_startdate->between($filter_start,$filter_end){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$contractCounter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if($propery->contract_startdate->between($filter_start,$filter_end){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;$otherCounter++;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; $byContractDate->put($user->id,$contractCounter);&nbsp; &nbsp; $byOther->put($user->id,$otherCounter);}在您看来:<table>&nbsp; <thead>&nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp;<th>User</th>&nbsp; &nbsp; &nbsp; &nbsp;<th>Property (start date)</th>&nbsp; &nbsp; &nbsp; &nbsp;<th>Property (other date)</th>&nbsp; &nbsp; &nbsp; &nbsp;<th>Offers</th>&nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp;</thead>&nbsp; &nbsp;<tbody>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp;@foreach($users as $user)&nbsp; &nbsp; &nbsp; &nbsp;<tr>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{$user->name}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{$byContract->get($user->id)}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{$byOther->get($user->id)}}</td>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<td>{{$user->offers->count()}}</td>&nbsp; &nbsp; &nbsp; &nbsp;</tr>&nbsp; &nbsp;@endforeach&nbsp; &nbsp;</tbody></table>
随时随地看视频慕课网APP
我要回答