渴求式加载指定字段、加载多个关联关系、嵌套的渴求式加载、带条件约束的渴求式加载

渴求式加载多个关联关系

有时候你需要在单个操作中渴求式加载多个不同的关联关系。要实现这个功能,只需要添加额外的参数到 with 方法即可:

1 $books = AppBook::with('author', 'publisher')->get();

嵌套的渴求式加载

要渴求式加载嵌套的关联关系,可以使用”.“语法。例如,我们在一个 Eloquent 语句中渴求式加载所有书的作者及所有作者的个人联系方式:

1 $books = AppBook::with('author.contacts')->get();

渴求式加载指定字段

并不是每次获取关联关系时都需要所有字段,因此,Eloquent 允许你在关联查询时指定要查询的字段:

1 $users = AppBook::with('author:id,name')->get(); 

注:使用这个特性时,id 字段是必须列出的。

带条件约束的渴求式加载

有时候我们希望渴求式加载一个关联关系,但还想为渴求式加载指定更多的查询条件:

1 $users = AppUser::with(['posts' => function ($query) {
2     $query->where('title', 'like', '%first%');
3 }])->get();

在这个例子中,Eloquent 只渴求式加载 title 包含 first 的文章。当然,你还可以调用其它查询构建器来自定义渴求式加载操作:

1 $users = AppUser::with(['posts' => function ($query) {
2     $query->orderBy('created_at', 'desc');
3 }])->get();

一表关联二表,二表关联三表,查询三表指定字段(多级关联查询指定字段,中间表要查询关联的字段 orderid,orderinfo_id,[orderid是ordercode关联order的字段,orderinfo_id是ordercode关联Orderinfo的字段]

 1 $this->_model->with('businessSettlement:id,start_time,end_time','ordercode:id,user_time,orderid,orderinfo_id','ordercode.order:id,type,order_no','ordercode.OrderInfo:id,product_price')->SettlementBusinessId($id)->chunk(1000, function ($dataTypeContent,$sn) {
 2 
 3     foreach($dataTypeContent as $key=>$item)
 4     {
 5         $arr = [];
 6         $arr[] = $key + 1 + (($sn - 1) * 1000);
 7         //訂單類型(1、福利,2、預約,3、特殊福利)
 8         if (isset($item->ordercode->order->type) && $item->ordercode->order->type == 1) {
 9             $arr[] = '福利';
10         } elseif(isset($item->ordercode->order->type) && $item->ordercode->order->type == 2) {
11             $arr[] = '預約';
12         } elseif(isset($item->ordercode->order->type) && $item->ordercode->order->type == 3) {
13             $arr[] = '特殊福利';
14         } else {
15             $arr[] = '--';
16         }
17         //訂單號
18         $arr[] = isset($item->ordercode->order->order_no) ? $item->ordercode->order->order_no."	" : '--';
19         $arr[] = $item->vouchercode."	";//核銷碼
20         $arr[] = $item->billamount ? number_format($item->billamount,1)."	" : "0.0";//交易金額
21         $arr[] = $item->commission ? '-'.number_format($item->commission,1)."	" : "0.0";//佣金
22         //結算金額(扣減佣金)
23         $arr[] = $item->merchantsettleamount ? number_format($item->merchantsettleamount,2)."	" : "0.00";
24         $arr[] = isset($item->ordercode->user_time) ? $item->ordercode->user_time : 'YYYY-MM-DD HH:MM:SS';//核銷時間
25         $arr[] = $item->storename;//核銷門店
26         $arr[] = $item->vouchername;//福利名稱
27         //福利價格訂金(如有)
28         $arr[] = isset($item->ordercode->OrderInfo->product_price) ? $item->ordercode->OrderInfo->product_price : '--';
29         //結算週期(由)
30         $arr[] = isset($item->businessSettlement->start_time) ? $item->businessSettlement->start_time : '--';
31         //結算週期(止)
32         $arr[] = isset($item->businessSettlement->end_time) ? $item->businessSettlement->end_time : '--';
33 
34         $this->SetCellData($arr);
35         unset($arr);
36         unset($item);
37     }
38     $sn = $sn + 1;
39     unset($dataTypeContent);
40 });