嵌套JSON数据Laravel 5中的WHERE子句 [英] WHERE Clause in nested json data Laravel 5

查看:443
本文介绍了嵌套JSON数据Laravel 5中的WHERE子句的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

作为我上一篇帖子中的跟进问题,当您有嵌套的json数据时,我只是想不出如何使用WHERE子句.请参阅我最后的帖子作为参考. >

因此,我在模型中使用关系生成了此类数据:

[
  {
     "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问题 .现在,我只想过滤创建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问题作为参考.

解决方案

此代码似乎有效:

$regions = Region::with(['branches.user.pendings' => function($query){
    $query->where('created_at', 'like', '%2016-07-12%');
}])->get();

请确认这是最佳做法还是有更好的方法.

As a follow up question in my last post, I just can't figure out how to use WHERE clause when you have a nested json data. Please see my last post as reference to this.

So, I have this kind of data generated by using relationship in my models:

[
  {
     "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"
                 }
              ]
           }
        }

What I'm trying to do is loop all regions with branches and get the pending of each branches. I'm successfully done the looping with my last SO question. Now, I just want to filter the date when the pending was created.

I'm trying to do this:

$regions = Region::with(array('branches->user->pendings' => function($query) {
    $query->where('created_at', '=', '2016-07-14 09:48:55');
}))->get();

But I'm getting this error:

BadMethodCallException in Builder.php line 2345:
Call to undefined method Illuminate\Database\Query\Builder::branches->user->pendings()

I've also done some research but end up asking here. Thanks in advance and please don't forget to see my last SO question as your reference.

解决方案

This code seems to work:

$regions = Region::with(['branches.user.pendings' => function($query){
    $query->where('created_at', 'like', '%2016-07-12%');
}])->get();

Please confirm if it is a best practice or there's a better way to do it.

这篇关于嵌套JSON数据Laravel 5中的WHERE子句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆