Error:You must specify an orderBy clause when using this function

Error:You must specify an orderBy clause when using this function

我在尝试迁移时遇到以下错误。实际上,我有很多数据,所以我使用了 laravel.

中的块

我正在使用 MongoDB 而不是 SQL

** You must specify an orderBy clause when using this function.**

我的迁移文件的up函数是

    public function up()
                {
        ini_set('max_execution_time', 250); 
        ini_set('memory_limit', '1024M'); 
        $product_array = DB::collection('product')->orderBy('created_at','asc');
        $product_array->chunk(20, function($product_array)
            {       
        $cotationPercentForecast=0;
        $cotationPercent=0;
        $quote_tpl = array();
        foreach ($product_array as $key => $value) {
        if (isset($value['quote_tpl'])) {
        $quote_tpl = $value['quote_tpl'];
        $defaultScoreCount = 0;
        $realScoreCount = 0;
        $forecastScoreCount = 0;
        foreach ($quote_tpl as $idxPerimetre => $perimetre) {
        if (isset($perimetre['families'])) {
        foreach ($perimetre['families'] as $idxFamilies => $family) {
        if (isset($family['subfamilies'])) {
        foreach ($family['subfamilies'] as $idxSubFamilies => $subfamily) {
        if (isset($subfamily['items'])) {
        foreach ($subfamily['items'] as $idxitem => $item) {
        if (isset($item['criterias'])) {
        foreach ($item['criterias'] as $idxcriteria => $criteria) {
                if (!isset($criteria['inactive']) || $criteria['inactive'] != 'true') {
                        $defaultScoreCount++;
                        if (isset($criteria['real_score'])) {
                          $realScoreCount++;
                        }
                        if (isset($criteria['forecast_score'])) {
                          $forecastScoreCount++;
                        }
                      }
        } // FOREACH CRITERIA
        } // ISSET CRITERIA ### 
        } // FOREACH ITEM                                    
        } // ISSET ITEM ###
        } // FOREACH SUBFAMILY  
        } // ISSET SUBFAMILY  ###
        } // FOREACH FAMILY 
        } // ISSET FAMILY ### 
        }// FOREACH PERIMETER ##
    if ($realScoreCount > 0 && $defaultScoreCount > 0) {
     $cotationPercentUn = ($realScoreCount / $defaultScoreCount) * 100;
     $cotationPercent = round($cotationPercentUn, 2);
    }
    if ($forecastScoreCount > 0 && $defaultScoreCount > 0) {
    $cotationPercentUnForecast = ($forecastScoreCount / $defaultScoreCount) * 100;
     $cotationPercentForecast = round($cotationPercentUnForecast, 2);
    }

     $results = DB::collection('product')->update(['cotation_percent' => $cotationPercent,'forecast_cotation_percent' => $cotationPercentForecast]);

        }

        }
        });
        }

我在这里看了很多答案,几乎所有的答案都建议使用 orderBy,所以我使用了它。但仍然出现错误。

我用过

 Product::chunk(5, function($product_array){}

感谢任何帮助。提前致谢。

当 Laravel 内部使用 Illuminate/Database/Query/Builder 内部定义的 enforceOrderBy 时添加此警告。

每当您在由 DB facade Illuminate\Database\Query\Builder 直接实例化的查询构建器上使用块时,它会询问您:

You must specify an orderBy clause when using this function.

所以如果你这样做就会发生:

\DB::table('product')->chunk(10, function($product){
    ...
});

如果您手动向其添加订单,它将不会抛出错误并且会按预期工作:

\DB::table('product')->orderBy('created_at')->chunk(10, function($product){
    ...
});

但是,最好直接使用 Eloquent 模型,这样不会强制您手动添加 order by 子句:

Product::chunk(10, function($product){
    ...
});

也没有方法 DB::collection(),如果您愿意,可以使用 DB::table() 代替,除非您使用 mongodb 而不是 MySQL