0

我被困在这一点上,我测试了很多东西,看不到发生了什么,我需要创建一个预订摘要,但我从我的表的关系中得到空数据:
res_reservations key = idReservation
res_reservation_rooms key = saver

两个表中的关系是 idReservation

预订模型类

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");
      }
 }

预订房间样板房

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");
  }
}

我试图调用与其他关系相同但仍然返回空房间数组

我的问题是关于我做错了什么

4

1 回答 1

0

看起来很混乱,我假设您的房间每个房间都链接到多个预订。

理想情况下,我们将有两个模型,

型号\OBKE\房间

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

型号\OBKE\预订

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

现在

Models\OBKE\Rooms::get(1) 为您提供单个房间对象

Models\OBKE\Rooms::get(1)->reservations,为您提供链接到房间的预订对象数组

于 2018-11-17T20:07:51.797 回答