Home > Blockchain >  How to add two get() - Laravel
How to add two get() - Laravel

Time:09-11

I have two get() :

 $nearest_drivers = Driver::where('active', 1)->where('approve', 1)->where('available', 1)->where('vehicle_type', $type_id)->whereIn('id', $nearest_driver_ids)->whereNotIn('id', $meta_drivers2)->limit(10)->get();
 $nearest_drivers2 = Driver::where('active', 1)->where('approve', 1)->where('available', 1)->where('vehicle_type', $type_id)->whereIn('id', $nearest_driver_ids)->whereNotIn('id', $meta_drivers)->limit(10)->get();

How do I add $nearest_drivers2 inside $nearest_drivers ?

Like $nearest_drivers = $nearest_drivers2

CodePudding user response:

I think you need union. https://laravel.com/docs/9.x/queries#unions

        $nearest_drivers = Driver::where([
                'active' => 1,
                'approve' => 1,
                'available' => 1,
                'vehicle_type' => $type_id,
            ])
            ->whereIn('id', $nearest_driver_ids)
            ->whereNotIn('id', $meta_drivers2)
            ->limit(10);


        $nearest_drivers2 = Driver::where([
                'active' => 1,
                'approve' => 1,
                'available' => 1,
                'vehicle_type' => $type_id,
            ])
            ->whereIn('id', $nearest_driver_ids)
            ->whereNotIn('id', $meta_drivers)
            ->union($nearest_drivers) // <-- here
            ->limit(10)
            ->get();

CodePudding user response:

you can merge collections like this:

$nearest_drivers->merge($nearest_drivers2)
  • Related