【问题标题】:Request a Laravel eloquent Structure or otherway [closed]请求 Laravel 雄辩的结构或其他方式[关闭]
【发布时间】:2020-03-25 07:32:59
【问题描述】:

enter image description here

你好, 我想请教您的意见。如您所见,我有这些表结构。我想预订内容。但在我的结构中,我无法获得真正的数据。

对于 order_id - 1 我得到 -> 预订 ID 1 res_name_1 预留 id 2 res_name_2(而不是额外的 id 2)

希望,我解释清楚了。

最好的问候。

穆拉特。

【问题讨论】:

    标签: laravel eloquent


    【解决方案1】:

    假设您有一个 Order 模型,并且您在其中建立了与 Reservation 模型的关系:

    $orderId = 1;
    
    // Get just the reservation for the order
    $reservation = Order::find($orderId)->reservation;
    
    // Get the order and the reservation.
    $reservation = Order::find($orderId)->with(['reservation'])->get();
    
    

    另一个进一步的假设是您在Order 模型中与Reservation 模型的关系如下所示:

        /**
         * Get the reservation record associated with the order.
         */
        public function reservation()
        {
            return $this->hasOne('App\Reservation');
        }
    

    另一个假设是您通过迁移在数据库订单表中设置了一个外键。

    $table->foreign('reservation_id')->references('id')->on('reservations');
    

    https://laravel.com/docs/master/eloquent#defining-models

    https://laravel.com/docs/master/eloquent-relationships

    https://laravel.com/docs/master/eloquent-relationships#one-to-one

    https://laravel.com/docs/master/migrations#foreign-key-constraints

    您的问题在代码方面非常模糊,只能用未知的假设来回答。

    【讨论】:

      猜你喜欢
      • 2020-06-21
      • 2016-03-18
      • 2013-05-07
      • 2021-12-24
      • 2019-06-18
      • 1970-01-01
      • 1970-01-01
      • 2015-09-27
      • 2018-04-13
      相关资源
      最近更新 更多