我有 3 张桌子 - 书籍、订单、物品
我将订单数据(order_id,user_id)保存在订单表中,书籍 ID 保存在项目表中,因为用户可以添加多本书。
图书
+++++++++++++++++++++++++++++
+ id book_code name +
+++++++++++++++++++++++++++++
1 abc Book 1
2 def Book 2
命令
+++++++++++++++++++++++++++++
+ id user_id +
+++++++++++++++++++++++++++++
1 1
项目
++++++++++++++++++++++++++++++++++
+ id order_id book_id quantity +
++++++++++++++++++++++++++++++++++
1 1 1 2
1 1 2 3
我试图显示在订单 ID 下预订的订单详细信息和书籍,但我对使用这些关系感到困惑。由于所有数据都存储在 Order 表中,因此我能够成功显示订单详细信息。但我无法显示订购到相应订单 ID 的书籍
模型书
public function items()
{
return $this->hasMany(Items::class);
}
模型订单
public function items()
{
return $this->hasMany(Items::class,'order_id');
}
控制器
public function user_order_details($payment_orderid)
{
// dd($payment_orderid); //payment_orderid = 1
$order_details = Orders::find($payment_orderid);
$order_products = Orders::find($payment_orderid)->items;
$books = Books::with('items')->get();
dd($books);
}
路线
Route::get('orders', 'UserDashboardController@user_order'); // I will be fetching the user orders based on the ID from Orders table
Route::get('orders/{payment_orderid}', 'UserDashboardController@user_order_details'); // Here I would like to show the details related to the order
我能够获取结果,但我不确定如何将结果与 order_id 隔离。
如果我的数据库实现错误且不可扩展,我愿意接受建议。
简而言之,我正在尝试显示相应订单的书籍详细信息
注意:如果投反对票,请确保您评论原因。对于过去的几个问题,我无缘无故地被否决了
墨色风雨
撒科打诨