Php 仅laravel过滤器嵌套条件
我只想过滤dealTransactions中的嵌套值 简单地说,Merchant Wands only dealTransactions提供了交易日期 我试过下面这样的方法,但不起作用Php 仅laravel过滤器嵌套条件,php,laravel,Php,Laravel,我只想过滤dealTransactions中的嵌套值 简单地说,Merchant Wands only dealTransactions提供了交易日期 我试过下面这样的方法,但不起作用 dates = ['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05']; $merchant = Merchant::with(['deals.dealTransactions'])->where('slug', $s
dates = ['2019-01-01', '2019-01-02', '2019-01-03', '2019-01-04', '2019-01-05'];
$merchant = Merchant::with(['deals.dealTransactions'])->where('slug', $slug)
->whereHas('deals.dealTransactions', function($query) use ($dates) {
foreach($dates as $date) {
$query->where('date', '=', $date);
}
})
->first();
deal_transactions table
id, deal_id, date
deals table
id, merchant_id,
merchants table
id, many columns for merchant
谢谢如果我正确理解了您的模式,这可能会有帮助:
// Here you define the scope that will be used to select & eager load.
$transactionsScope = function ($q) use ($dates) {
return $q->whereIn('date', $dates);
};
// All merchant of slug in/with that transaction's scope.
$merchant = Merchant::where('slug', $slug)
->whereHas('deals', function ($q) use ($transactionsScope) {
return $q->whereHas('dealTransactions', $transactionsScope);
})
->with(['deals' => function ($q) use ($transactionsScope) {
return $q->with('dealTransactions', $transactionsScope);
}])
->firstOrFail();
如果我正确理解了您的模式,这可能会有帮助:
// Here you define the scope that will be used to select & eager load.
$transactionsScope = function ($q) use ($dates) {
return $q->whereIn('date', $dates);
};
// All merchant of slug in/with that transaction's scope.
$merchant = Merchant::where('slug', $slug)
->whereHas('deals', function ($q) use ($transactionsScope) {
return $q->whereHas('dealTransactions', $transactionsScope);
})
->with(['deals' => function ($q) use ($transactionsScope) {
return $q->with('dealTransactions', $transactionsScope);
}])
->firstOrFail();
您应该能够通过嵌套关系上的“急切加载”约束来执行此操作:
$merchant = Merchant::where('slug', $slug)
->with(['deals.dealTransactions' => function ($query) use ($dates) {
$query->whereIn('date', $dates);
}])->first();
您应该能够通过嵌套关系上的“急切加载”约束来执行此操作:
$merchant = Merchant::where('slug', $slug)
->with(['deals.dealTransactions' => function ($query) use ($dates) {
$query->whereIn('date', $dates);
}])->first();
谢谢你的回复。根据你的回答,我有了一个想法。谢谢。谢谢你的回复。根据你的回答,我有了一个想法。谢谢。您只想加载dealTransaction日期与日期匹配的交易,或者您只想加载dealTransaction,如果它有匹配的日期?@lagbox我想加载交易和dealTransactions,但dealTransactions必须与日期匹配。谢谢,请再澄清一下。。。无论发生什么情况,您都希望加载交易,但这些交易中的交易必须只与这些日期匹配?@lagbox Yes完全正确!您只想加载dealTransaction日期与日期匹配的交易,或者您只想加载dealTransaction日期匹配的交易?@lagbox我想同时加载交易和dealTransactions,但dealTransactions必须与日期匹配。谢谢,请进一步澄清。。。无论发生什么情况,您都希望加载交易,但这些交易中的交易必须只与这些日期匹配?@lagbox Yes完全正确!