gpt4 book ai didi

laravel-4 - orWhereHas - Eloquent 查询上的参数分组 - 我如何在 Laravel 中执行此操作?

转载 作者:行者123 更新时间:2023-12-04 01:51:34 26 4
gpt4 key购买 nike

在我正在构建的 Eloquent 查询中,我对 has 施加了约束。使用 Laravel 4.1 的关系 whereHasorWhereHas方法。

在示例足球应用程序中,我希望对 homeClub 施加约束。和 awayClub关系,以便我可以结果集将选择 homeClub = 阿森纳或 awayClub = 阿森纳的位置。

此问题由 earlier question 演变而来似乎在使用 orWhereHas 时方法 - 生成的 sql 不会对 or 进行分组约束。

查询(放置约束的相关摘录):

$ret
->where( function( $subquery ) use ( $ret ){
$ret->whereHas('homeClub', function ( $query ){
$query->where('name','Arsenal' );
})->orWhereHas('awayClub',function ( $query ){
$query->where('name','Arsenal' );
});
})
->where( function ( $subquery ) use ( $ret, $parameterValues ){
$ret->whereHas('season', function ($query) use ( $parameterValues ){
$query->where('name', $parameterValues['season_names'] );

});
} )
->whereHas('territory',function( $query ) use ( $parameterValues ){
$query->where('region','Australia');

})->get()->toArray();

这会产生 sql:
SELECT * FROM `broadcasts` WHERE 

(SELECT count(*) FROM `uploads` WHERE `broadcasts`.`upload_id` = `uploads`.`id` and `type` = 'international-audience') >= '1'
and
(SELECT count(*) FROM `clubs` WHERE `clubs`.`id` = `broadcasts`.`home_club_id` and `name` = 'Arsenal') >= '1'
or
(SELECT count(*) FROM `clubs` WHERE `clubs`.`id` = `broadcasts`.`away_club_id` and `name` = 'Arsenal') >= '1'
and
(SELECT count(*) FROM `seasons` WHERE `broadcasts`.`season_id` = `seasons`.`id` and `name` = '2012/13') >= '1'
and
(SELECT count(*) FROM `territories` WHERE `broadcasts`.`territory_id` = `territories`.`id` and `region` = 'Australia') >= '1'

但是,这不是我想要的,因为提到 Eloquent 声明,俱乐部查询是 grouped上面的查询要么选择 homeClub 约束,要么选择 awayClub、赛季名称、领土区域。我想要的是以下 SQL:
SELECT * FROM `broadcasts` WHERE 

(SELECT count(*) FROM `uploads` WHERE `broadcasts`.`upload_id` = `uploads`.`id` and `type` = 'international-audience') >= '1'
and
((SELECT count(*) FROM `clubs` WHERE `clubs`.`id` = `broadcasts`.`home_club_id` and `name` = 'Arsenal') >= '1'
or
(SELECT count(*) FROM `clubs` WHERE `clubs`.`id` = `broadcasts`.`away_club_id` and `name` = 'Arsenal') >= '1' )
and
(SELECT count(*) FROM `seasons` WHERE `broadcasts`.`season_id` = `seasons`.`id` and `name` = '2012/13') >= '1'
and
(SELECT count(*) FROM `territories` WHERE `broadcasts`.`territory_id` = `territories`.`id` and `region` = 'Australia') >= '1'

注意.. club 子查询上的括号。

有谁知道我将如何将此写成 Eloquent 查询?我真的不想恢复到流利/连接。

最佳答案

您需要引用传递给 where 闭包的查询。否则,您将绕过任何分组将分组的 where 子句添加到主查询中:

$ret
->where( function( $query ){
$query->whereHas('homeClub', function ( $subquery ){
$subquery->where('name','Arsenal' );
})
->orWhereHas('awayClub',function ( $subquery ){
$subquery->where('name','Arsenal' );
});
})
->where( function ( $query ) use ( $parameterValues ){
$query->whereHas('season', function ($subquery) use ( $parameterValues ){
$subquery->where('name', $parameterValues['season_names'] );
});
})
->whereHas('territory',function( $query ) use ( $parameterValues ){
$query->where('region','Australia');
})
->get();

关于laravel-4 - orWhereHas - Eloquent 查询上的参数分组 - 我如何在 Laravel 中执行此操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21382870/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com