laravel-eloquent where子句来自关系表两层深

时间:2017-06-29 04:28:08

标签: laravel eloquent laravel-5.4

你如何处理这种情况? 在laravel这是直截了当的

$contracts = Contracts::with('bidinvitations')->with('bidinvitations.planentries)->where('created_at','2017-02-1')->get();

这样做的前提是,在 where子句旁边,列(例如 created_at )必须来自父表(合约)。

如何查询来自另一个表的where子句,例如

$contracts = Contracts::with('bidinvitations')->with('bidinvitations.planentries)->where('bidinvitations.planentries.created_at','2017-02-1')->get();

此查询: where(**'bidinvitations.planentries.created_at'**,'2017-02-1')

只是没有工作

1 个答案:

答案 0 :(得分:1)

你可以做这样的事情

$createdAt = '2017-02-1';
$contracts = Contracts::with(['bidinvitations' => function($bidinvitations_query) use($createdAt) {
                                $bidinvitations_query->with(['planentries' =>
                                    function($planentries_query) use($createdAt) {
                                        $planentries_query->where('created_at', $createdAt);
                                    }
                                ]);
                            }])->get();