English 中文(简体)
从任命表中获得职位的资格
原标题:Query to get available slot from appointments table

页: 1 详细情况如下:

Employees | working_hours                                                     | appointment
id, name  | id, day, start_time, end_time, employeeable_id, employeeable_type | id, employee_id, start_date_time, end_date_time

<><>Relation:

class Employee extends Model
{
   public function workingHours()
   {
      return $this->morphMany(WorkingHour::class,  employeeable );
   }
}

class WorkingHour extends Model
{
   public function employeeable()
   {
      return $this->morphTo();
   }
}

class Appointment extends Model
{
   public function employee()
   {
      return $this->belongsTo(Employee::class);
   }
}

www.un.org/spanish/ecosoc 雇员:

[
   { day: 1, start_time:  08:00:00 , end_time:  17:00:00  },
   ...
   { day: 5, start_time:  08:00:00 , end_time:  17:00:00 }
]

www.un.org/spanish/ga/president

问题:

如果从May 22, 2022June 1, 2022,到>,请提供的现有位置,那么我期望作出如下回应:

[
   {  2022-05-22 : [ 08:00 ,  08:30 ,  09:00 , ...,  17:00 ] },
   {  2022-05-23 : [ 08:00 ,  08:30 ,  09:30 ] } // 09:00 notice excluded.
   ...
   {  2022-06-01, [] }
]

如何界定上述问题? 我可以想到的是,从雇员A到每小时工作小时,如果时间不可用,则进行检查。

最佳回答

I suggest you don t handle time with queries. this is my solution:

public function index()
{
    $appointment = [
         id  => 1,
         name  =>  Appointment 1 ,
         start_date_time  =>  2022-05-23 09:00:00 ,
         end_date_time  =>  2022-05-23 09:30:00 
    ];

    // Employee A working hours
    $workingHours = collect([
        [ day  => 1,  start_time  =>  08:00:00 ,  end_time  =>  17:00:00 ],
        [ day  => 2,  start_time  =>  08:00:00 ,  end_time  =>  17:00:00 ],
        [ day  => 3,  start_time  =>  08:00:00 ,  end_time  =>  17:00:00 ],
        [ day  => 4,  start_time  =>  08:00:00 ,  end_time  =>  17:00:00 ],
        [ day  => 5,  start_time  =>  08:00:00 ,  end_time  =>  17:00:00 ],
        [ day  => 6,  start_time  =>  08:00:00 ,  end_time  =>  17:00:00 ],
        [ day  => 0,  start_time  =>  08:00:00 ,  end_time  =>  17:00:00 ], // carbon for sunday default is 0
    ]);

    $dateArray = [];
    $startDate = Carbon::parse( 2022-05-22 );
    $endDate = Carbon::parse( 2022-06-01 );

    while ($startDate->lte($endDate)) {
        // seach for working hours that match the day of the week
        $workingHour = (array) $workingHours->firstWhere( day , $startDate->dayOfWeek);

        // generate time for each day
        $times = $this->generateTimes($workingHour);

        // extract date from appoint start date time
        $appointmentDate = Carbon::parse($appointment[ start_date_time ])->format( Y-m-d );

        if ($appointmentDate === $startDate->format( Y-m-d )) {
            // remove time according to appointment time
            $times = $this->removeTime($times, $appointment);
        }

        // add time to date array
        $dateArray[$startDate->format( Y-m-d )] = $times;

        // increment date
        $startDate->addDay();
    }

    dd($dateArray);
}

private function generateTimes(array $workingHour)
{
    // the working time of the workers must be reduced by at least 1 hour.
    // because there is no way for you to have an appointment on your end working time.
    $startTime = Carbon::parse($workingHour[ start_time ]);
    $endTime = Carbon::parse($workingHour[ end_time ])->subHour();

    $times = [];
    while ($startTime->lte($endTime)) {
        $times[] = $startTime->format( H:i );
        $startTime->addMinutes(30);
    }

    return $times;
}

private function removeTime($times, $appointment)
{
    $startTime = Carbon::parse($appointment[ start_date_time ]);
    $endTime = Carbon::parse($appointment[ end_date_time ]);

    $startTime = $startTime->format( H:i );
    $endTime = $endTime->format( H:i );

    $times = array_diff($times, [$startTime, $endTime]);

    return $times;
}
问题回答

暂无回答




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...