Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I am getting the following error while trying to migrate. Actually, I have a lot of data so I used the chunk in laravel.

I am using MongoDB not SQL

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

My migration file's up function is is

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

        }

        }
        });
        }

I have gone through many answers here and almost all suggest to use orderBy so I used it. But still getting the error.

i have used

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

Any help is appreciated. Thanks in advance.

like image 833
Adesh Kumar Avatar asked Feb 20 '19 08:02

Adesh Kumar


1 Answers

This warning is added when Laravel internally uses enforceOrderBy which is defined inside Illuminate/Database/Query/Builder.

Whenever you use chunk on a query builder instantiated by DB facade Illuminate\Database\Query\Builder directly, it would ask you :

You must specify an orderBy clause when using this function.

So it will happen if you are doing :

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

If you manually add an order by to this, it will not throw the error and will work as expected :

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

However, its better to use the Eloquent Model directly like which will not enforce you to add an order by clause manually :

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

Also there is no method DB::collection(), you can use DB::table() instead if you wish, unless you are using mongodb and not MySQL

like image 172
Mihir Bhende Avatar answered Sep 18 '22 11:09

Mihir Bhende