根据日期范围显示不同的结果

时间:2021-07-19 12:50:02

标签: php mysql laravel

抱歉标题模糊,但不知道如何描述。

我有一个前端页面,它显示了预订网站上的所有地点,并显示了今天可以预订的地方。

我添加了一个日历范围和一个提交按钮,当管理员选择两个日期并提交时,它会将其发送到后端,并显示这些日期范围而不是今天的可用性。

日期范围选择器代码:

 <form action="{{ route('admin.place.viewplaces') }}" method="POST">
    <input type="date" name="startDate" value=""/>
    <input type="date" name="endDate" value=""/>
    <button type="submit" name="dateSearch">Apply</button>

查看地点的后端功能:

public function place_view(Request $request)
{
    $startDate = $request->startDate;
    $endDate = $request->endDate;
    $startDate = date('Y-m-d');
    $endDate = date('Y-m-d');
    $places = Place::orderBy('place_id')->get();
    $booking = new Booking;
    $Todaydate = date('Y-m-d');
    foreach ($places as $place) {
        if ($place->status == -1) {
            continue;
        }
        $place->status = $booking->place_is_available($place->place_id, $startDate, $endDate);
        $place->status = $booking->place_is_available_subs($place->place_id, $startDate, $endDate, $place->status);
    }

    return view('adminpages.viewplaces')->with('places', $places);
}

以及检查该地点是否可用于当前日期 ara 的两个函数:

  public function place_is_available($place_id, $userCheckin, $usercheckout)
  {

    $got1dayeirlier = date('Y-m-d', strtotime("-1 day", strtotime($userCheckin)));
    $got1daylater = date('Y-m-d', strtotime("+15 day", strtotime($userCheckin)));

    $temp_book = new TempBooking;
    if ($temp_book->isBusy($place_id, $userCheckin)) return 2;
    // ToDo try to make lower complexity
    $bookings_exits = Booking::where('place_id', $place_id)
      // ->where('user_checkin', '>', $got1dayeirlier)
      // ->orWhere('user_checkin', '<', $got1daylater)
      // ->where('user_checkout', '>=', $usercheckout)
      ->where('is_approved', 1)->get();

    foreach ($bookings_exits as $booking) {
      if ($userCheckin >= $booking->user_checkin && $userCheckin <= $booking->user_checkout)
        return 2;
      if ($usercheckout >= $booking->user_checkin && $usercheckout <= $booking->user_checkout)
        return 2;
      if ($userCheckin <= $booking->user_checkin && $usercheckout >= $booking->user_checkout)
        return 2;
    }
    return 0;
  }

第二:

  public function place_is_available_subs($place_id, $userCheckin, $usercheckout, $status)
  {
    // make all returns 2 replace with 1 for gray
    $temp_book = new TempBooking;
    if ($temp_book->isBusy($place_id, $userCheckin)) return $status;

    $bookings_exits = Booking::where('place_id', $place_id)
      ->where('is_approved', 1)
      ->where('user_payment_type', 'Agreements')->get();
    foreach ($bookings_exits as $booking) {
      if ($userCheckin >= $booking->user_checkin && $userCheckin <= $booking->user_checkout)
        return 2;
      if ($usercheckout >= $booking->user_checkin && $usercheckout <= $booking->user_checkout)
        return 2;
      if ($userCheckin <= $booking->user_checkin && $usercheckout >= $booking->user_checkout)
        return 2;
    }
    return $status;
  }

由于某种原因,该功能无法正常工作,我觉得我遗漏了一些东西,但我无法理解它,因为页面仍然显示今天的可用性,而不是提供的两个日期。< /p>

我会很感激你的第二视角,也许能帮助我理解我在这里遗漏了什么。

0 个答案:

没有答案