To create our database seeder, we will modify the DatabaseSeeder class that extends Seeder. The name of the file is database/seeds/DatabaseSeeder.php. The contents of the file will be as follows:

<?php

use IlluminateDatabaseSeeder;
use IlluminateDatabaseEloquentModel;

class DatabaseSeeder extends Seeder {

    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        Model::unguard();

        //create a user
        $user = new MyCompanyUser();
        $user->id=1;
        $user->email = "[email protected]";
        $user->password = Hash::make('[email protected]');
        $user->save();

        //create a country
        $country = new MyCompanyAccommodationLocationState;
        $country->name = "United States";
        $country->id = 236;
        $country->save();

        //create a state
        $state = new MyCompanyAccommodationLocationState;
        $state->name = "Pennsylvania";
        $state->id = 1;
        $state->save();

        //create a city
        $city = new MyCompanyAccommodationLocationCity;
        $city->name = "Pittsburgh";
        $city->save();

        //create a location
        $location = new MyCompanyAccommodationLocation;
        $location->city_id = $city->id;
        $location->state_id = $state->id;
        $location->country_id = 236;
        $location->latitude = 40.44;
        $location->longitude = 80;
        $location->code = '15212';
        $location->address_1 = "100 Main Street";
        $location->save();

        //create a new accommodation
        $accommodation = new MyCompanyAccommodation;
        $accommodation->name = "Royal Plaza Hotel";
        $accommodation->location_id = $location;
        $accommodation->description = "A modern, 4-star hotel";
        $accommodation->save();

        //create a room
        $room1 = new MyCompanyAccommodationRoom;
        $room1->room_number= 'A01';
        $room1->accommodation_id = $accommodation->id;
        $room1->save();

        //create another room
        $room2 = new MyCompanyAccommodationRoom;
        $room2->room_number= 'A02';
        $room2->accommodation_id = $accommodation->id;
        $room2->save();

        //create the room array
        $rooms = [$room1,$room2];
        
    }

}

The seeder file sets up the very basic possible scenario. For initial testing, we don’t need to even add every country, state, city, and location possible to the database; we simply need to add the essential information to create various scenarios. To create a new reservation; for example, we will create an instance of each of the user, country, state, city, location, and accommodation models, and then create two rooms, which are added to an array of rooms.

Let’s create a repository for the reservation that will implement a very simple repository interface:

<?php

namespace MyCompanyAccommodation;

interface RepositoryInterface {
    public function create($attributes);
}

Now let’s create ReservationRepository, which implements RepositoryInterface:

<?php

namespace MyCompanyAccommodation;

class ReservationRepository implements RepositoryInterface {
    private $reservation;

    function __construct($reservation)
    {
        $this->reservation = $reservation;
    }

    public function create($attributes)
    {
        $this->reservation->create($attributes);
        return $this->reservation;
    }
}

Now, we will create the method that is needed to create the reservation and also to populate the pivot table, reservation_room:

public function create($attributes)
{

    $modelAttributes= array_except($attributes, ['rooms']);

    $reservation = $this->reservationModel->create($modelAttributes);
    if (isset($attributes['rooms']) ) {
        $reservation->rooms()->sync($attributes['rooms']);
    }
    return $reservation;
}

Tip

The array_except() Laravel helper is used to return the attributes array, except for the $rooms array, which will be used for the sync() function.

Here, we set each attribute of the model to the attributes that are set in the method. We will need to add the method that will establish the many-to-many relationship between the reservations and rooms:

public function rooms(){
    return $this->belongsToMany('MyCompanyAccommodationRoom')->withTimestamps();
}

In this case, we need to add withTimestamps() to the relationship so that the timestamps will be updated, indicating when the relationship was saved in the reservation_room pivot table.

Advertisements