Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何与laravel上的WHERE in HASMANY关系求和?谢谢你的帮助_Sql_Laravel_Eloquent - Fatal编程技术网

Sql 如何与laravel上的WHERE in HASMANY关系求和?谢谢你的帮助

Sql 如何与laravel上的WHERE in HASMANY关系求和?谢谢你的帮助,sql,laravel,eloquent,Sql,Laravel,Eloquent,晚上好,朋友们,我很难从一个与某个条件有很多关系的人那里得到一个和 我试着这样做: $marketPlaces=ModelAccountMarketplace::with(['orders']) ->whereHas('orders',function($query)use($dates){ $query->selectRaw('SUM(valor_frete)作为somaFreteGratis') ->其中('datetime',[$dates['dateStart'],$dates['dat

晚上好,朋友们,我很难从一个与某个条件有很多关系的人那里得到一个

我试着这样做:

$marketPlaces=ModelAccountMarketplace::with(['orders'])
->whereHas('orders',function($query)use($dates){
$query->selectRaw('SUM(valor_frete)作为somaFreteGratis')
->其中('datetime',[$dates['dateStart'],$dates['dateEnd']]);
});
但是当我试图通过以下方式获取值:
var\u dump($marketPlace->somaFreteGratis)

我得到一个
null
值。我尝试将where放入
WITH()
中,如下所示:

$marketPlaces = ModelAccountMarketplace::with(['orders' => function ($query) {
    $query->selectRaw('SUM(valor_frete) as somaFreteGratis')->where('tipo_frete', 'gratis');
}])
->whereHas('orders', function ($query) use ($dates) {
    $query->whereBetween('datetime', [$dates['dateStart'], $dates['dateEnd']]);
});
但在每次尝试中,当我检查时,都会得到一个空值:



任何帮助都将不胜感激。

您可以使用
with count
闭包来完成您想要做的事情

$marketPlaces=ModelAccountMarketplace::withCount([
“订单作为somaFreteGratis”=>函数($query)使用($dates){
$query->select(DB::raw('sum(valor_frete)'))
->其中('tipo_frete','gratis')
->其中('datetime',[$dates['dateStart'],$dates['dateEnd']]);
}
])
->get();

我也尝试过,但由于某种原因,当我尝试使用$marketPlaces[0]时返回0->somaFreteGratis…mysql上有两行valor_frete,其中tipo_frete=gratis,但感谢您的帮助抱歉,对于订单为tipo_frete=gratis的市场,返回整数1,但应返回浮点值的总和,对吗?此查询返回所有市场及其someFreteGratis。如果您想要特定的市场,请e、 尝试将
->get()
替换为
->find(id)
。然后您可以直接使用
$marketPlaces->somaFreteGratis