首页 > 解决方案 > 单个查询是否可以处理 if 语句

问题描述

单个查询可以做到这一点吗

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

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

标签: laraveleloquent

解决方案


有时,这些步骤可以避免多个条件查询。

  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');
      }
    }

推荐阅读