在我上一篇post中的后续问题中,我就是不明白当您有一个嵌套的json数据时如何使用WHERE子句。请参阅我的上一篇post作为参考。
因此,我在模型中使用关系生成了这种类型的数据:
[
{
"id":1,
"name":"South Luzon",
"branches":[
{
"id":1,
"machinenum":108,
"name":"Alaminos",
"region_id":1,
"user":{
"id":52,
"name":"",
"email":"baic@alaminosbranch.addessacorp",
"role":0,
"machinenum":108,
"created_at":"2016-07-11 05:58:04",
"updated_at":"2016-07-14 09:49:00",
"pendings":[
{
"id":10,
"user_id":52,
"region":"South Luzon",
"branch":"Alaminos",
"docdate":"2016-07-14 00:00:00",
"ls_or":12345,
"por":1,
"ls_ci":12345,
"ci":2,
"ls_ch":12345,
"ch":2,
"dep":5,
"cla":0,
"grpo":3,
"si":25,
"so":62,
"sts":2,
"disb":3,
"arcm":5,
"apcm":65,
"pint":2,
"rc_cash":1,
"reason":"Test Reason Alaminos",
"created_at":"2016-07-14 09:48:55",
"updated_at":"2016-07-14 09:48:55"
}
]
}
}我要做的是用branches循环所有的regions,并获得每个分支的pending。我用我的最后一个SO question成功地完成了循环。现在,我只想过滤pending的创建日期。
我正在尝试这样做:
$regions = Region::with(array('branches->user->pendings' => function($query) {
$query->where('created_at', '=', '2016-07-14 09:48:55');
}))->get();但是我得到了这个错误:
BadMethodCallException in Builder.php line 2345:
Call to undefined method Illuminate\Database\Query\Builder::branches->user->pendings()我也做了一些研究,但最终还是在这里问了。提前感谢,请别忘了看看我上一篇SO question作为你的推荐信。
发布于 2016-07-21 09:20:52
这段代码似乎可以工作:
$regions = Region::with(['branches.user.pendings' => function($query){
$query->where('created_at', 'like', '%2016-07-12%');
}])->get();请确认这是最佳实践,还是有更好的方法。
发布于 2016-07-20 11:51:38
$regions = App\Region::with(['pendings' => function ($query) {
$query->where('created_at', '=', '2016-07-14 09:48:55');
}])->get();发布于 2016-07-20 16:02:58
在输出到json之前,你不能先在关系上这样做吗?您可以在那里构建此查询,因此json输出将是干净的。你之前的帖子有一个答案,可能会让你找到解决方案。
https://stackoverflow.com/questions/38471609
复制相似问题