简体   繁体   中英

Eloquent 5.7 Relationships return empty

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM