0

i am stuck at this point, i tested a lot of things and can't see what is happening, i need to create a reservation summary but i get empty data from the relationship my tables are:
res_reservations key = idReservation
res_reservation_rooms key = saver

relation in bouth table is idReservation

Reservation Model class

namespace Models\OBKE;
use Illuminate\Database\Eloquent\Model;
class Reservation extends Model{
      public $timestamps    = true;
      const CREATED_AT      = 'createdDate';
      const UPDATED_AT      = 'updatedDate';
      public $incrementing  = false;  
      protected $table      = "res_reservations";
      protected $primaryKey = "idReservation";
      protected $fillable = ["idReservation",
                             "resortName",
                             "programName",
                             "contract",
                             "lead",
                             "booker",
                             "total",
                             "idStatus",
                             "createdBy",
                             "updatedBy",
                             "currency",
                             "front",
                             "discount",
                             "pr_code"];

      public function rooms(){
        return $this->hasMany('\Models\OBKE\Reservation\Rooms',"idReservation","idReservation");
      }
 }

Reservation rooms model class

namespace Models\OBKE\Reservation;
use Illuminate\Database\Eloquent\Model;
class Rooms extends Model{

  public $timestamps = false;
  protected $table = "res_reservation_rooms";
  protected $primaryKey = "saver";
  protected $fillable=  [
    "idReservation",
    "row",
    "date",
    "idResort",
    "resortName",
    "idProgram",
    "programName",
    "idRoom",
    "roomName",
    "adults",
    "adultsRate",
    "juniors",
    "juniorsRate",
    "kids",
    "kidsRate",
    "infants",
    "infantsRate",
    "total",
    "discount"
  ];

  public function rooms(){
    return $this->belongsTo('\Models\OBKE\Reservation',"idReservation","idReservation");
  }
}

i tried to call same as other relationships but still return empty rooms array

my question is about what i did wrong

1 Answers1

0

It looks confusing, I'm assuming that you have rooms with each room linked to multiple reservations.

Ideally, we will have two models,

Models\OBKE\Rooms

namespace Models\OBKE;
use Illuminate\Database\Eloquent\Model;
class Rooms extends Model{
    public function reservations(){
       return $this->hasMany('\Models\OBKE\Reservation',"idReservation","idReservation");
    }
}

Models\OBKE\Reservation

namespace Models\OBKE;
    use Illuminate\Database\Eloquent\Model;
    class Reservation extends Model{
        public function room(){
           return $this-> belongsTo('\Models\OBKE\Rooms',"idReservation","idReservation");
        }
    }

Now

Models\OBKE\Rooms::get(1) gives you a single room object

Models\OBKE\Rooms::get(1)->reservations, gives you an array of reservation objects linked to the room

Community
  • 1
  • 1
Jeevan
  • 309
  • 1
  • 6
  • a reservation can have multiple rooms Models\OBKE\Reservation object has the basic reservation info Models\OBKE\Rooms has de admin part o the rooms not the reservation rooms list – Alejandro Hernandez Nov 19 '18 at 15:18
  • Does it mean that every reservation can have one or more rooms associated to it and every room can be associated with one or more reservations ? In which case, it is a many to many relationship and I can update the answer according to that. – Jeevan Nov 19 '18 at 15:59
  • i have a catalogue of rooms "Models\OBKE\Rooms" and i have rooms associated to a reservation "Models\OBKE\Reservation\Rooms" – Alejandro Hernandez Nov 20 '18 at 17:37