laravel多表关联查询问题

我有三张表:1.order_invoices订单表2.order_invoice_productions订单明细表3.productions商品表
字段说明:order_invoice订单表
id主键id
order_sn订单号
money订单金额
...
order_invoice_productions订单明细
id主键ID
invoice_id关联订单表中id
production_id商品id,关联商品表id
....
productions商品表
id商品id
cate商品分类
title商品名称
price商品价格
...
问题:1.订单详情,由订单id获得订单的详情,订单与订单明细信息,同时包含商品的名称,分类等等商品属性2.订单列表,同样包含订单,订单明细,商品属性的信息
这样的查询该怎么写,在order_invoice模型中hasmany了order_invoice_productions;请各位大大解答一下...谢谢!
修改
OrderInvoices::where('id',$invoice_id)->with(['orderInvoiceProductions'=>function($query){
$query->with(['production'=>function($query){
$query->select('title','category_alias');
}]);//->select('id','production_id','discounted_price','discount_id','cop_reward')->get();
}])->get();
返回值:
{
"id":1,
"order_sn":"XT2017071908070144062",
"total_price":"4800.00",
"deleted_at":null,
"created_at":"2017-07-1908:01:45",
"updated_at":"2017-07-1909:51:13",
"order_invoice_productions":[
{
"id":9,
"order_invoice_id":1,
"order_sn":"XT2017071908070144062",
"production_id":1,
"deleted_at":null,
"created_at":"2017-07-1909:35:49",
"updated_at":"2017-07-1909:51:13",
"production":null
},
{
"id":10,
"order_invoice_id":1,
"production_id":2,
"deleted_at":null,
"created_at":"2017-07-1909:35:49",
"updated_at":"2017-07-1909:51:13",
"production":null
}
]
}
ABOUTYOU
浏览 1005回答 2
2回答

肥皂起泡泡

可能有问题,找出他们之间的关系就好了//根据订单id查出订单详情publicfunctiongetOrderDetail($oid){returnOrderDetail::where('order_id',$oid)->get();}//在订单详情Model写好关联关系publicfunctionproductions(){return$this->belongsTo(Productions::class);}//视图@foreach($order_detailas$order)商品名:{{$order->productions->name}}@endforeach
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript