为了账号安全,请及时绑定邮箱和手机立即绑定

laravel多表关联查询问题

laravel多表关联查询问题

ABOUTYOU 2019-05-25 14:39:21
我有三张表:1.order_invoices订单表2.order_invoice_productions订单明细表3.productions商品表字段说明:order_invoice订单表id主键idorder_sn订单号money订单金额...order_invoice_productions订单明细id主键IDinvoice_id关联订单表中idproduction_id商品id,关联商品表id....productions商品表id商品idcate商品分类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}]}
查看完整描述

2 回答

?
肥皂起泡泡

TA贡献1829条经验 获得超6个赞

可能有问题,找出他们之间的关系就好了
//根据订单id查出订单详情
publicfunctiongetOrderDetail($oid)
{
returnOrderDetail::where('order_id',$oid)->get();
}
//在订单详情Model写好关联关系
publicfunctionproductions()
{
return$this->belongsTo(Productions::class);
}
//视图
@foreach($order_detailas$order)
商品名:{{$order->productions->name}}
@endforeach
                            
查看完整回答
反对 回复 2019-05-25
  • 2 回答
  • 0 关注
  • 1004 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信