Benjamest

Experience

190

0 Best Reply Awards

  • Member Since 2 Months Ago
  • 0 Lessons Completed
  • 0 Favorites

23rd September, 2018

Benjamest left a reply on Project Ideas • 3 weeks ago

Can you draw inspiration from your line of work or a hobby? My work is unrelated to web development and therefore there are many examples at work I find where a web app would help. I can then create a solution to develop my skills (regardless as to whether it is used or not)

19th September, 2018

Benjamest started a new conversation AssertSee Related Field • 3 weeks ago

Hi all,

I'm trying to assert that the related field name of room->name is visible however I am getting the following error:

InvalidArgumentException: Unable to locate factory with name [default] [App\Models\Room].

I imagine the error is because the factory is not eager loading the relation but I'm not sure how best to eager load for the test as per my controller. My test function:

    /** @test */
    public function a_user_can_view_a_booking()
    {
        $booking = factory('App\Models\Booking')->create();
        $response = $this->get('/booking/'.$booking->id);

        $response->assertSee($booking->id);
        $response->assertSee($booking->room->name);
        $response->assertSee($booking->date);
        $response->assertSee($booking->start_time);
        $response->assertSee($booking->end_time);
    }
}

The room table is already pre-seeded and the factory works correctly. I can see the room name when I view the page after populating in tinker but unsure how to write the test. The factory consists of:

'room_id' => getInstanceOf(Room::class),

function getInstanceOf($class, $returnIdOnly = true) {
        $instance = $class::inRandomOrder()->first() ?? factory($class)->create();
        return $returnIdOnly ? $instance->id : $instance;
    }

12th August, 2018

Benjamest left a reply on Generate A Room Booking Recurrence And Then Find Empty Rooms For Each Booking • 2 months ago

Ok, so I've been racking my brains quite a bit and what I've come up with is:

  1. Generate the recurrences
  2. Query the database for all rooms in the department
  3. Create an array for each date containing the rooms room
  4. Query the database for all bookings which fall in the datetime range (see below)
  5. For each booking's date & room flag the relevant field in step 3 as room unavailable.
  6. Output all rooms by booking with available/unavailable rooms flagged appropriately.

This works on the assumption that there isn't multiple recurrences on the same day (there isn't). It will take it down to two queries instead of a single query for each booking. Any comments welcome.

P.s. how do people name the different levels of an eloquent query? In this one I've gone $query -> $q -> oh shit, now what... alphabet? $r -> $s

        Clinic::where(function ($query) use ($Dates)
        {
            $query->where(function($q) use ($Dates)
            {
                $q->where( function($r) use ($Dates)
                {
                    $i=0;
                    foreach($Dates as $Date)
                    {
                        if($i == 0)
                        {
                            $r->where( function($s) use ($Date)
                            {
                                $s->where('StartTime', '>=', $Date->getStart()->format('Y-m-d H:i'));
                                $s->where('StartTime', '<', $Date->getEnd()->format('Y-m-d H:i'));
                            });
                            $i++;
                        } else {
                            $r->orWhere( function($s) use ($Date)
                            {
                                $s->where('StartTime', '>=', $Date->getStart()->format('Y-m-d H:i'));
                                $s->where('StartTime', '<', $Date->getEnd()->format('Y-m-d H:i'));
                            });
                            $i++;
                        }
                    }
                });
                $q->where( function($r)
                {
                    $r->where('ClinicStatus', '=', 1);
                    $r->orWhere('ClinicStatus', '=', 2);
                    $r->orWhere('ClinicStatus', '=', 4);
                });
            });
            $query->orWhere(function($q) use ($Dates)
            {
                $q->where( function($r) use ($Dates)
                {
                    $i=0;
                    foreach($Dates as $Date)
                    {
                        if($i == 0)
                        {
                            $r->where( function($s) use ($Date)
                            {
                                $s->where('EndTime', '>', $Date->getStart()->format('Y-m-d H:i'));
                                $s->where('EndTime', '<=', $Date->getEnd()->format('Y-m-d H:i'));
                            });
                            $i++;
                        } else {
                            $r->orWhere( function($s) use ($Date)
                            {
                                $s->where('EndTime', '>', $Date->getStart()->format('Y-m-d H:i'));
                                $s->where('EndTime', '<=', $Date->getEnd()->format('Y-m-d H:i'));
                            });
                            $i++;
                        }
                    }
                });
                $q->where( function($r)
                {
                    $r->where('ClinicStatus', '=', 1);
                    $r->orWhere('ClinicStatus', '=', 2);
                    $r->orWhere('ClinicStatus', '=', 4);
                });
            });
            $query->orWhere(function($q) use ($Dates)
            {
                $q->where( function($r) use ($Dates)
                {
                    $i=0;
                    foreach($Dates as $Date)
                    {
                        if($i == 0)
                        {
                            $r->where( function($s) use ($Date)
                            {
                                $s->where('StartTime', '<=', $Date->getStart()->format('Y-m-d H:i'));
                                $s->where('EndTime', ">=", $Date->getEnd()->format('Y-m-d H:i'));
                            });
                            $i++;
                        } else {
                            $r->orWhere( function($s) use ($Date)
                            {
                                $s->where('StartTime', '<=', $Date->getStart()->format('Y-m-d H:i'));
                                $s->where('EndTime', ">=", $Date->getEnd()->format('Y-m-d H:i'));
                            });
                            $i++;
                        }
                    }
                });
                $q->where( function($r)
                {
                    $r->where('ClinicStatus', '=', 1);
                    $r->orWhere('ClinicStatus', '=', 2);
                    $r->orWhere('ClinicStatus', '=', 4);
                });
            });
        })->whereHas('Room.Outpatient', function($q) use($OutpatientID) {
            $q->where('id', "=", $OutpatientID);
        })->get();

11th August, 2018

Benjamest left a reply on Generate A Room Booking Recurrence And Then Find Empty Rooms For Each Booking • 2 months ago

Thanks again, I appreciate your contribution. I had not thought of trying to find examples through an airline booking system which should help widen my searching. Should I find a suitable answer I will ensure to copy it here.

Benjamest left a reply on Generate A Room Booking Recurrence And Then Find Empty Rooms For Each Booking • 2 months ago

Thanks jlrdw. I've read far-and-wide on this one, even generic SQL resources. I guess if if I went more theoretical with the question what I'm trying to ask is can I query similar to the above on multiple date ranges in one query and get a list of rooms, instead of one query per date range.

Benjamest started a new conversation Generate A Room Booking Recurrence And Then Find Empty Rooms For Each Booking • 2 months ago

I'm creating a booking system in Laravel 5.6 whereby I have a form which takes the booking details and the recurrence requirement, usually weekly bookings for 2 years+ (100+ recurrences), and then I want it to return each date with a list of rooms available. So far in the application I can generate the recurrences and can find empty rooms for a single booking, but I want an efficient way of querying multiple bookings for available rooms without having to do a single query for each recurrence. Is this possible?

The room availability query:

$AvailableRooms = Derpartment::where('id', $DepartmentID)->with(['Rooms' => function($Rooms) use ($StartTime, $EndTime)
    {
        $Rooms->whereDoesntHave('Clinics', function ($Clinics) use ($StartTime, $EndTime) {
            $Clinics->where(function($q) use ($StartTime, $EndTime)
            {
                $q->where( function($r) use ($StartTime, $EndTime)
                {
                    // Req Start before or equal to booked start & Req End is before booked End time
                    $r->where('StartTime', '>=', $StartTime);
                    $r->where('StartTime', '<', $EndTime);
                });
                $q->where( function($r)
                {
                    $r->where('ClinicStatus', '=', 1);
                    $r->orWhere('ClinicStatus', '=', 2);
                    $r->orWhere('ClinicStatus', '=', 4);
                });
            });
            $Clinics->orWhere(function($q) use ($StartTime, $EndTime)
            {
                $q->where( function($r) use ($StartTime, $EndTime)
                {
                    // Req Start is before booked end & Req End is after booked end
                    $r->where('EndTime', '>', $StartTime);
                    $r->where('EndTime', '<=', $EndTime);
                });
                $q->where( function($r)
                {
                    $r->where('ClinicStatus', '=', 1);
                    $r->orWhere('ClinicStatus', '=', 2);
                    $r->orWhere('ClinicStatus', '=', 4);
                });
            });
            $Clinics->orWhere(function($q) use ($StartTime, $EndTime)
            {
                $q->where( function($r) use ($StartTime, $EndTime)
                {
                    // Req Start equal or greater than Booked Start & Req End is
                    $r->where('StartTime', '<=', $StartTime);
                    $r->where('EndTime', ">=", $EndTime);
                });
                $q->where( function($r)
                {
                    $r->where('ClinicStatus', '=', 1);
                    $r->orWhere('ClinicStatus', '=', 2);
                    $r->orWhere('ClinicStatus', '=', 4);
                });
            });
        });
    }])->get();

ClinicStatus is how I determine whether the clinic should be included or not (e.g. other booking in database is cancelled)

The recurrence generator is done using SimShaun's Recurr from which I get an array of dates.

Edit Your Profile
Update

Want to change your profile photo? We pull from gravatar.com.