skip to Main Content

I want Data From Both Queries One After Another

->when($todayDate, function ($query) use ($todayDate) {
       $query->whereDate('startdate', '>=', $todayDate)->orderBy('batches.startdate', 'desc');
})->when($todayDate, function ($query) use ($todayDate) {
       $query->whereDate('startdate', '<=', $todayDate)->orderBy('batches.startdate', 'desc');
})

4

Answers


  1. Chosen as BEST ANSWER
    $packages1 = Package::select('packages.*')->join('package_prices', 'packages.id', '=', 'package_prices.package_id')
                    ->Join('package_itineraries', 'packages.id', '=', 'package_itineraries.package_id')
                    ->leftJoin('batches', 'packages.id', '=', 'batches.package_id')
                    ->when($states, function ($query) use ($states) {
                        $states = Collection::make(explode(",", $states));
                        $query->whereIn('state_id', $states);
                    })
                    ->when($countries, function ($query) use ($countries) {
                        $countries = Collection::make(explode(",", $countries));
                        $query->whereIn('country_id', $countries);
                    })
    
                    ->when($difficulty, function ($query) use ($difficulty) {
                        $difficulty = Collection::make(explode(",", $difficulty));
                        $query->whereIn('trek_difficulty_id', $difficulty);
                    })
    
                    ->when($category, function ($query) use ($category) {
                        $query->where('category_id', $category);
                    })
                    ->when($prices, function ($query) use ($prices) {
                        $query->priceBetween($prices);
                    })
                    ->when($batches, function ($query) use ($batches) {
                        $query->batchesBetween($batches);
                    })
                    ->when($duration, function ($query) use ($duration) {
                        $query->DurationBetween($duration);
                    })
                    ->when($startingLocations, function ($query) use ($startingLocations) {
                        $query->Startinglocations($startingLocations);
                    })
                    ->when($columnName, function ($query) use ($columnName, $columnSortOrder) {
                        if ($columnName == "price") {
                            $query->where('package_prices.is_default', 1)->orderBy('package_prices.price', $columnSortOrder);
                        } elseif ($columnName == "duration") {
                            $query->orderBy('package_itineraries.duration', $columnSortOrder);
                        }
                        $query->orderBy($columnName, $columnSortOrder);
                    })
                    ->when($todayDate, function ($query) use ($todayDate) {
                        $query->whereDate('batches.startdate', '>=', $todayDate)->orderBy('batches.startdate', 'asc');
                    });
    

  2. `$todayDate = now()->toDateString();
    
    $upcoming = DB::table('table_name')
    ->when($todayDate, function ($query) use ($todayDate) {
        $query->whereDate('startdate', '>=', $todayDate)
            ->orderBy('batches.startdate', 'desc');
    });
    
    $past = DB::table('table_name')
    ->when($todayDate, function ($query) use ($todayDate) {
        $query->whereDate('startdate', '<=', $todayDate)
            ->orderBy('batches.startdate', 'desc');
    });
    
    $results = $upcoming->union($past)->get();'
    
    '->when($todayDate, function ($query) use ($todayDate) { 
    $query->orderBy('startdate', 'desc')
         ->orderByRaw("startdate >= '{$todayDate}' desc");
    })`
    
    Login or Signup to reply.
  3. In Laravel, you can use the union() method to combine the results of two queries. Here’s an example:

    $todayDate = date('Y-m-d');
    
    $query1 = DB::table('table_name')
        ->when($todayDate, function ($query) use ($todayDate) {
            $query->whereDate('startdate', '>=', $todayDate)
                ->orderBy('batches.startdate', 'desc');
        });
    
    $query2 = DB::table('table_name')
        ->when($todayDate, function ($query) use ($todayDate) {
            $query->whereDate('startdate', '<=', $todayDate)
                ->orderBy('batches.startdate', 'desc');
        });
    
    $results = $query1->union($query2)->get();
    
    Login or Signup to reply.
  4. This is How we can do without Union

     $case = "CASE WHEN batches.startdate >= CURDATE() THEN 1 ELSE 2 END";
    
    
     ->orderByRaw($case)
                    ->orderBy('batches.startdate')
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search