Laravel雄辩-如何使用雄辩的渴望负载求和?

Laravel雄辩-如何使用雄辩的渴望负载求和?,laravel,Laravel,我要连接多个表来绘制图表。各表如下:- segments -id -internal_segment brands -id -segment_id items -id -brand_id clearance -id -item_id -calendar_id calendar -id 我在我的模型中创建了这些关系- class Segment extends Model { publ

我要连接多个表来绘制图表。各表如下:-

segments    
-id    
-internal_segment

brands    
-id    
-segment_id

items    
-id    
-brand_id

clearance    
-id    
-item_id    
-calendar_id

calendar
-id
我在我的模型中创建了这些关系-

    class Segment extends Model
    {
        public function brand()
        {
            return $this->hasMany('clearance_data_analytics\Brand');
        }
    }
    
    class Brand extends Model
    {
       public function segment()
       {
          return $this-> belongsTo('clearance_data_analytics\Segment');
       }

       public function item()
       {
          return $this->hasMany('clearance_data_analytics\Item');
       }
    }
    
    class Item extends Model
    {

    public function clearance()
    {
        return $this->hasMany('clearance_data_analytics\Clearance');
    }

    public function brand(){
        return $this-> belongsTo('clearance_data_analytics\Brand');
    }
   }

   class Clearance extends Model
   {
    protected $table = 'clearance';

    public function calendar()
    {
        return $this->belongsTo('clearance_data_analytics\Calendar');
    }

    public function item()
    {
        return $this->belongsTo('clearance_data_analytics\Item');
    }

    }

    class Calendar extends Model
    {
    protected $table = 'calendar';

    public function clearance()
    {
        return $this->hasMany('clearance_data_analytics\Clearance');
    }
    }
现在,我尝试使用Elounting编写以下查询-

SELECT
    internal_segment,
    SUM(figure)
FROM
    segments
LEFT JOIN brands ON segments.id = brands.segment_id
LEFT JOIN items ON  brands.id = items.brand_id
LEFT JOIN clearance ON items.id = clearance.item_id
LEFT JOIN calendar ON calendar.id = clearance.calendar_id
WHERE calendar.calendar_year= 2020 and calendar.calendar_period = 07
GROUP BY
    segments.internal_segment
到目前为止,我已经在我的控制器中写入了这些。但是我不知道怎样才能算出总数并分组使用

 public function index()
    {
        //$seg_data = Segment::with('brand.item.clearance')->get();

        $seg_data = Segment::with(['brand.item.clearance'=> function($query){
                                    $query->select(DB::raw('sum(figure) as total'))
                                }])
                                ->get()
                                ->toArray();
    }

有人能帮忙吗?

只是一个建议:既然有一个条件适用于位于嵌套关系末尾的日历,为什么不在eloquent中使用联接呢

$segments = Segment::select('internal_segment', DB::raw('SUM(figure) as total'))
                     ->leftJoin('brands', 'segments.id', 'brands.id')
                     ->leftJoin('items', 'brands.id', 'items.brand_id')
                     ->leftJoin('clearance', 'items.id', 'clearance.item_id')
                     ->leftJoin('calendar', 'calendar.id', 'clearance.calendar_id')
                     ->where('calendar.calendar_year', 2020)
                     ->where('calendar.calendar_period', 7)
                     ->groupBy('segments.internal_segment')
                     ->get(); 

这回答了你的问题吗?我需要按内部_段分组。但是如果我在查询生成器中使用GROUPBY,结果将显示null。谢谢您的建议。