Benjamest

Benjamest

Member Since 3 Months Ago

Experience Points 190
Experience
Level
Lessons Completed 0
Lessons
Completed
Best Reply Awards 0
Best Answer
Awards
  • Start Your Engines Achievement

    Start Your Engines

    Earned once you have completed your first Laracasts lesson.

  • First Thousand Achievement

    First Thousand

    Earned once you have earned your first 1000 experience points.

  • One Year Member Achievement

    One Year Member

    Earned when you have been with Laracasts for 1 year.

  • Two Year Member Achievement

    Two Year Member

    Earned when you have been with Laracasts for 2 years.

  • Three Year Member Achievement

    Three Year Member

    Earned when you have been with Laracasts for 3 years.

  • Four Year Member Achievement

    Four Year Member

    Earned when you have been with Laracasts for 4 years.

  • Five Year Member Achievement

    Five Year Member

    Earned when you have been with Laracasts for 5 years.

  • School In Session Achievement

    School In Session

    Earned when at least one Laracasts series has been fully completed.

  • Welcome To The Community Achievement

    Welcome To The Community

    Earned after your first post on the Laracasts forum.

  • Full Time Learner Achievement

    Full Time Learner

    Earned once 100 Laracasts lessons have been completed.

  • Pay It Forward Achievement

    Pay It Forward

    Earned once you receive your first "Best Reply" award on the Laracasts forum.

  • Subscriber Achievement

    Subscriber

    Earned if you are a paying Laracasts subscriber.

  • Lifer Achievement

    Lifer

    Earned if you have a lifetime subscription to Laracasts.

  • Laracasts Evangelist Achievement

    Laracasts Evangelist

    Earned if you share a link to Laracasts on social media. Please email [email protected] with your username and post URL to be awarded this badge.

  • Chatty Cathy Achievement

    Chatty Cathy

    Earned once you have achieved 500 forum replies.

  • Laracasts Veteran Achievement

    Laracasts Veteran

    Earned once your experience points passes 100,000.

  • Ten Thousand Strong Achievement

    Ten Thousand Strong

    Earned once your experience points hits 10,000.

  • Laracasts Master Achievement

    Laracasts Master

    Earned once 1000 Laracasts lessons have been completed.

  • Laracasts Tutor Achievement

    Laracasts Tutor

    Earned once your "Best Reply" award count is 100 or more.

  • Laracasts Sensei Achievement

    Laracasts Sensei

    Earned once your experience points passes 1 million.

  • Top 50 Achievement

    Top 50

    Earned once your experience points ranks in the top 50 of all Laracasts users.

23 Sep
2 months ago

Benjamest left a reply on Project Ideas

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)

19 Sep
2 months ago

Benjamest started a new conversation AssertSee Related Field

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;
    }
12 Aug
3 months ago

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

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();
11 Aug
3 months ago

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

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

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

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.