0

任何人都可以帮忙,我没有想法。

我有这个代码:

$item = ItemsBrandOitb::select('ItmsGrpCod')->where('Brand', '=', $brand)->first();

return ItemsOitm::where('Country', $country)
            ->where('OnHand', '>', 0)
            ->where('ItmsGrpCod','=', $item->ItmsGrpCod)
            ->with([
               'price' => function($q) use ($country){
                  return $q->where('Country', $country);
               },
               'stock'=> function($q) use ($country){
                   return $q->where('Country', $country);
               },
               'brand' => function($q) use ($brand){
                   return $q->whereHas('Brand', '=', $brand);
               }
           ]
       )->groupBy('U_GeralRef')->orderBy('ColectionDate', 'desc')->get();

有谁知道如何提出这个查询:

$item = ItemsBrandOitb::select('ItmsGrpCod')->where('Brand', '=', $brand)->first();

//here:

->where('ItmsGrpCod','=', $item->ItmsGrpCod)

所以我只能有一个数据库查询?

谢谢

4

2 回答 2

0

尝试这样的事情:

return ItemsOitm::where('Country', $country)
        ->where('OnHand', '>', 0)
        ->where('ItmsGrpCod', function ($query) use ($brand) {
            $query->from('TABLE_NAME')
                ->where('Brand', '=', $brand);
        })
        ->with([

                'price' => function( $q) use ($country){ 
                    return $q->where('Country', $country);
                },
                'stock'= > function( $q) use ($country){ 
                    return $q->where('Country', $country);
                },
                'brand' => function( $q) use ($brand){ 
                    return $q->whereHas('Brand', '=', $brand);
                }
            ]
        )->groupBy('U_GeralRef')->orderBy('ColectionDate', 'desc')->get();  
于 2019-04-02T12:43:15.590 回答
0

搞定了:

return ItemsOitm::where('Country', $country)
            ->where('OnHand', '>', 0)
            ->where('ItmsGrpCod', function ($query) use ($brand) {
                $query->select('ItmsGrpCod')->from('items_brand_oitbs')
                    ->where('Brand', $brand);
            })
            ->with([
                    'price' => function($q) use ($country){
                        return $q->where('Country', $country);
                    },
                    'stock'=> function($q) use ($country){
                        return $q->where('Country', $country);
                    }
                ]
            )->groupBy('U_GeralRef')->orderBy('ColectionDate', 'desc')->get();

必须删除最后一个急切加载(品牌)我不需要它,因为我已经在子查询中获得了值

感谢所有帮助过的人。干杯!

于 2019-04-02T16:04:20.287 回答