DeliveryAppointmentEvent.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <?php
  2. namespace App\Events;
  3. use App\DeliveryAppointmentCar;
  4. use Illuminate\Broadcasting\Channel;
  5. use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Foundation\Events\Dispatchable;
  8. use Illuminate\Queue\SerializesModels;
  9. class DeliveryAppointmentEvent implements ShouldBroadcast
  10. {
  11. use Dispatchable, SerializesModels;
  12. public $delivery;
  13. /**
  14. * Create a new event instance.
  15. *
  16. * @param DeliveryAppointmentCar|\stdClass $delivery
  17. *
  18. * @return void
  19. */
  20. public function __construct(DeliveryAppointmentCar $delivery)
  21. {
  22. $delivery->load(["deliveryAppointment"=>function($query){
  23. /** @var Builder $query */
  24. $query->withCount("cars");
  25. }]);
  26. $count = $delivery->deliveryAppointment->cars_count ?? 0;
  27. $delivery->warehouse = $delivery->deliveryAppointment->warehouse_id ?? "";
  28. $delivery->cubic_meter = isset($delivery->deliveryAppointment->cubic_meter) ? ($count>1 ? $delivery->deliveryAppointment->cubic_meter."/".$count : $delivery->deliveryAppointment->cubic_meter) : "";
  29. $delivery->tonne = isset($delivery->deliveryAppointment->tonne) ? ($count>1 ? $delivery->deliveryAppointment->tonne."/".$count : $delivery->deliveryAppointment->tonne) : "";
  30. $this->delivery = $delivery->withoutRelations();
  31. }
  32. /**
  33. * Get the channels the event should broadcast on.
  34. *
  35. * @return \Illuminate\Broadcasting\Channel|array
  36. */
  37. public function broadcastOn()
  38. {
  39. return new Channel('delivery');
  40. }
  41. public function broadcastAs()
  42. {
  43. return "car";
  44. }
  45. }