data !== null) return $this->data; $data = DB::table('show_dates') ->leftJoin('seats', 'seats.date', '=','show_dates.date') ->havingRaw('COUNT(`seats`.`id`) < '.$this->seats) ->whereRaw('`show_dates`.`date` > CURDATE()') ->where('show_dates.show_id', '=', $this->id) ->select('show_dates.date', DB::raw('COUNT(`seats`.`id`) AS `filled_seats`')) ->groupBy('show_dates.date'); if($uuid !== null) $data = $data->whereRaw( "`show_dates`.`date` NOT IN (SELECT date FROM seats WHERE uuid='".$uuid."' AND show_id='".$this->id."')"); $data = $data->get()->all(); $dates = []; foreach ($data as $row) array_push($dates, [ 'date' => $row->date, 'free_seats' => $this->seats - $row->filled_seats ]); $this->data = $dates; return $dates; } }