Возможна ли обработка одного запроса, если операторы - PullRequest
0 голосов
/ 06 мая 2020

Может ли это сделать один запрос

Schedule::where('location_id', $this->location_id)      //if result found do nothing else

      // but if no result found then evaluate
      wherisNull(location_id)

Ответы [ 2 ]

0 голосов
/ 06 мая 2020

Иногда с помощью этих шагов можно избежать множественных условных запросов.

  1. Сохранить результат запроса в переменной. Результатом является коллекция.
  2. Используйте where и другие помощники коллекции для ограничения коллекции.

Единственный запрос и помощники коллекции могут сделать трюк.

Так я и сделал.

Запрос

$appointment_at = $this->appointment_at;

    if(request()->ismethod("Post") || request()->ismethod("Patch")) {
      $schedules = Schedule::whereHas('dayTimes', function ($query) use ($appointment_at) {
         $query->where('start_time', '<=', $appointment_at->format('H:m'))
          ->where('end_time', '>=', $appointment_at->format('H:m'));
        })
        ->whereDate('start_date', '<=', $appointment_at)
        ->whereDate('end_date', '>=', $appointment_at)
      ->get();
    }

Использование помощников по сбору

    if(empty($this->location_id) && empty($this->service_id) && empty($this->user_id)) {
        $schedule = $schedules;
    }
    else if(!empty($this->location_id) && empty($this->service_id) && empty($this->user_id)) {
      $schedule = $schedules->where('location_id', $this->location_id);

      if(! $schedule->count()) {
        $schedule = $schedules->whereNull('location_id');
      }
    }
    else if(empty($this->location_id) && !empty($this->service_id) && empty($this->user_id)) {
      $schedule = $schedules->where('service_id', $this->service_id);

      if(! $schedule->count()) {
        $schedule = $schedules->whereNull('service_id');
      }
    }
    else if(empty($this->location_id) && empty($this->service_id) && !empty($this->user_id)) {
      $schedule = $schedules->where('user_id', $this->user_id);

      if(! $schedule->count()) {
        $schedule = $schedules->whereNull('user_id');
      }
    }
    else if(!empty($this->location_id) && !empty($this->service_id) && empty($this->user_id)) {
      $schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id);

      if(! $schedule->count()) {
        $schedule = $schedules->where('location_id', $this->location_id)->whereNull('service_id');
      }

      if(! $schedule->count()) {
        $schedule = $schedules->whereNull('location_id');
      }
    }
    else if(!empty($this->location_id) && empty($this->service_id) && !empty($this->user_id)) {
      $schedule = $schedules->where('location_id', $this->location_id)->where('user_id', $this->user_id);

      if(! $schedule->count()) {
        $schedule = $schedules->where('location_id', $this->location_id)->whereNull('user_id');
      }

      if(! $schedule->count()) {
        $schedule = $schedules->whereNull('location_id');
      }
    }
    else if(empty($this->location_id) && !empty($this->service_id) && !empty($this->user_id)) {
      $schedule = $schedules->where('service_id', $this->service_id)->where('user_id', $this->user_id);

      if(! $schedule->count()) {
        $schedule = $schedules->where('service_id', $this->service_id)->whereNull('user_id');
      }

      if(! $schedule->count()) {
        $schedule = $schedules->whereNull('service_id');
      }
    }
    else if(!empty($this->location_id) && !empty($this->service_id) && !empty($this->user_id)) {
      $schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id)->where('user_id', $this->user_id);

      if(! $schedule->count()) {
        $schedule = $schedules->where('location_id', $this->location_id)->where('service_id', $this->service_id)->whereNull('user_id');
      }

      if(! $schedule->count()) {
        $schedule = $schedules->where('location_id', $this->location_id)->whereNull('service_id')->whereNull('user_id');
      }

      if(! $schedule->count()) {
        $schedule = $schedules->whereNull('location_id');
      }
    }
0 голосов
/ 06 мая 2020

Здесь может помочь метод exists().

$schedule = Schedule::where('location_id', $this->location_id);

if(!$schedule->exists()){
   unset($query->where('location_id'));
   $schedule->wherisNull('location_id');
}

$schedule->get();
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...