-
| I want get filtered collection between to date and store it into some variable. then use this variable into another functions to group by and ... for avoiding each function filtered entire collection by date and group by. my code is $result = $query->whereRaw([
        '$and' =>
        $searchArray
       ]
);$groupByResults = $result->raw(function ($collection) {
    return $collection->aggregate([
        [
            '$group' => [
                "_id" => ['$substr' => ['$hs_code', 0, 2]],
                "success_num" => ['$sum' => '$total_value_usd'],
            ],
        ],
        [
            '$sort' => [
                "success_num" => -1,
            ],
        ],
        [
            '$facet' => [
                "data" => [['$skip' => 0 * 10], ['$limit' => 10]],
            ],
        ],
    ]);
});and the  | 
Beta Was this translation helpful? Give feedback.
      
      
          Answered by
          
            GromNaN
          
      
      
        Aug 31, 2023 
      
    
    Replies: 1 comment
-
| You cannot use  Try moving the  $groupByResults = $result->raw(function ($collection) {
    return $collection->aggregate([
        [
            '$match' => $searchArray,
        ],
        [
            '$group' => [
                "_id" => ['$substr' => ['$hs_code', 0, 2]],
                "success_num" => ['$sum' => '$total_value_usd'],
            ],
        ],
        [
            '$sort' => [
                "success_num" => -1,
            ],
        ],
        [
            '$facet' => [
                "data" => [['$skip' => 0 * 10], ['$limit' => 10]],
            ],
        ],
    ]);
}); | 
Beta Was this translation helpful? Give feedback.
                  
                    0 replies
                  
                
            
      Answer selected by
        GromNaN
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment
  
        
    
You cannot use
Query\Builder::raw(Closure)with other methods of the query builder.Try moving the
wherepart into a$matchstage.