请求Laravel雄辩的结构或其他方式

问题描述 投票:-2回答:1

enter image description here

你好,我想问你的建议。如您所见,我具有这些表结构。我想预定内容。但是在我的结构中,我无法获得真正的数据。

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

我希望,我清楚地解释了。

最好的问候。

Murat。

laravel eloquent
1个回答
0
投票

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

$orderId = 1;
Order::findOrFail($orderId)->reservation;

另一个进一步的假设是您在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

您的问题对于您的代码用途非常含糊,只能在未知的假设下回答。

© www.soinside.com 2019 - 2024. All rights reserved.