【发布时间】:2020-03-25 07:32:59
【问题描述】:
你好, 我想请教您的意见。如您所见,我有这些表结构。我想预订内容。但在我的结构中,我无法获得真正的数据。
对于 order_id - 1 我得到 -> 预订 ID 1 res_name_1 预留 id 2 res_name_2(而不是额外的 id 2)
希望,我解释清楚了。
最好的问候。
穆拉特。
【问题讨论】:
你好, 我想请教您的意见。如您所见,我有这些表结构。我想预订内容。但在我的结构中,我无法获得真正的数据。
对于 order_id - 1 我得到 -> 预订 ID 1 res_name_1 预留 id 2 res_name_2(而不是额外的 id 2)
希望,我解释清楚了。
最好的问候。
穆拉特。
【问题讨论】:
假设您有一个 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
您的问题在代码方面非常模糊,只能用未知的假设来回答。
【讨论】: