【问题标题】:Determine status of multiple collection objects确定多个集合对象的状态
【发布时间】:2021-01-10 20:24:53
【问题描述】:

我基本上有一个报告模型。一个报表有一个 ChartGroup,一个 ChartGroup 可以有多个 Chart。我基本上需要知道何时准备好查看报告,而这可能发生在至少一个图表已被处理时。

然后我需要知道报告何时可以下载,也就是报告的所有图表都已处理完毕的时间。这是我删除了一些列以减少代码的基本架构结构。

Schema::create('reports', function (Blueprint $table) {
    $table->id();

    $table->boolean('isSubmitted')->nullable()->default(false);
    $table->boolean('isViewable')->nullable()->default(false);
    $table->boolean('reportReady')->nullable()->default(false);
});

Schema::create('chart_group', function (Blueprint $table) {
    $table->id();
    $table->foreignId('report_id');

    $table->foreign('report_id')
        ->references('id')
        ->on('reports')
        ->onDelete('cascade');
});

Schema::create('charts', function (Blueprint $table) {
    $table->id();
    $table->string('chart_name')->nullable();
    $table->boolean('chart_processed')->default(false);
    $table->foreignId('chart_group_id');

    $table->foreign('chart_group_id')
        ->references('id')
        ->on('chart_group')
        ->onDelete('cascade');
});

然后在 Laravel 工作中,我得到所有报告并循环它们。然后我检查图表以获取报告,如果尚未处理,我将处理它。如果处理成功,我会更新图表并将其标记为已处理(其他代码)。

foreach ($reports as $report) {
    foreach ($report->chartGroup->chart as $chart) {
        if (!$chart->report_processed) {
            if ($this->queryStatus($chart)) {
                if (!$report->isViewable) {
                    $report->isViewable = true;
                    $report->saveOrFail();
                }
            }
        }
    }
}

所以到目前为止,如果报表有一个需要处理的图表,则它工作正常,并且该图表被标记为这样。我遇到的问题是这个。一份报告可能有 5 个图表。我需要一种方法来确定所有图表的处理时间,以便我可以将报告标记为准备就绪。

我该怎么做呢?

谢谢

【问题讨论】:

    标签: laravel laravel-5 eloquent


    【解决方案1】:

    您可以运行另一个作业来识别所有图表都已处理的报告,如下所示。

    我在这里所做的只是检查

    报告的可用图表计数,然后检查计数是否匹配 到该报告的 isViewable 真实计数。

    这意味着所有图表都已处理。如果这些计数匹配,我会将报告添加到空集合中。最后您可以根据需要处理$reports_to_be_downloaded 收藏。

    $reports_to_be_downloaded = collect();
    
    foreach ($reports as $report) {
      if($report->chartGroup->chart->count() == $report->chartGroup->chart()->where('isViewable',true)->count()){
        $reports_to_be_downloaded->add($report);
      } 
    }
    

    【讨论】:

      猜你喜欢
      • 2019-04-11
      • 2014-01-30
      • 1970-01-01
      • 1970-01-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多