public function assignNearestDriver() { $drivers = $this->dispatcher->drivers()->whereHas('orders', function ($q) { $q->where('status', '!=', 2); }, '=', 0)->has('locations')->get(); $min_user = 0; $min_dist = 0; if (count($drivers) == 0) { return; } foreach ($drivers as $driver) { $dist = Bing::distanceBetweenTwoPoints($driver->locations[0]->lat, $driver->locations[0]->lng, $this->lat, $this->lng); if ($min_dist > $dist) { $min_dist = $dist; $min_user = $driver; } if ($min_user == 0) { $min_user = $driver; $min_dist = $dist; } } $this->driver()->associate($min_user); }
public function assignNearestOrderIfAvailable() { if (!$this->is_driver()) { return; } if ($this->orders()->where('status', '!=', '2')->count() > 0) { return; } $orders = $this->dispatcher->orders()->whereStatus(0)->whereDriverId(null)->get(); $min_order = 0; $min_dist = 0; if (count($orders) == 0 || !$this->locations()->exists()) { return; } foreach ($orders as $order) { $dist = Bing::distanceBetweenTwoPoints($this->locations[0]->lat, $this->locations[0]->lng, $order->lat, $order->lng); if ($min_dist > $dist) { $min_dist = $dist; $min_order = $order; } if (!$min_order instanceof Order && $min_order == 0) { $min_order = $order; $min_dist = $dist; } } $this->orders()->save($min_order); }