Добавить результат отношения к запросу - PullRequest
0 голосов
/ 02 апреля 2019

Может кто-нибудь помочь, у меня нет идей.

У меня есть этот код:

$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)

То есть у меня может быть только один запрос к базе данных?

Спасибо

Ответы [ 2 ]

0 голосов
/ 02 апреля 2019

прибил его:

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();

Пришлось удалить последнюю загруженную загрузку (Бренд) Мне это не нужно, потому что я уже получаю значение в подзапросе

Спасибо всем, кто помог. Ура! * * 1006

0 голосов
/ 02 апреля 2019

Попробуйте что-то вроде этого:

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();  
...