忽略急切加载的数据的空对象 - laravel

Posted

技术标签:

【中文标题】忽略急切加载的数据的空对象 - laravel【英文标题】:ignore null objects of eager loaded data- laravel 【发布时间】:2015-06-30 00:20:41 【问题描述】:

我有以下 ORM 返回的 JSON

[

"id": 3,
"name": "Card Department",
"name_burmese": "Card Department",
"is_active": "1",
"created_at": "2014-11-23 07:02:07",
"updated_at": "2014-11-23 07:02:07",
"orm_bank_contact": []
,

"id": 1,
"name": "Loan Department",
"name_burmese": "Loan Department",
"is_active": "1",
"created_at": "2014-11-23 07:01:16",
"updated_at": "2015-02-24 09:05:35",
"orm_bank_contact": []
,

"id": 4,
"name": "Remittance Department",
"name_burmese": "Remittance Department",
"is_active": "0",
"created_at": "2015-02-24 09:43:25",
"updated_at": "2015-04-17 12:26:07",
"orm_bank_contact": []
,

"id": 2,
"name": "Deposit Department",
"name_burmese": "Deposit Department",
"is_active": "1",
"created_at": "2014-11-23 07:01:34",
"updated_at": "2015-04-20 14:04:27",
"orm_bank_contact": [

"id": 27,
"bank_department": 2,
"mobile": "9843139168",
"phone": "9843139168",
"email": "shresthabeenu@gmail.com",
"contact_person": "Binu Shrestha",
"address": "No. 416, Mahabandoola Road, Kyauktada Township, Yangon, Myanmar",
"is_active": "1",
"created_at": "2015-04-15 08:50:16",
"updated_at": "2015-04-15 08:50:16",
"bank_id": 13

]

]

但我只需要

[


"id": 2,
"name": "Deposit Department",
"name_burmese": "Deposit Department",
"is_active": "1",
"created_at": "2014-11-23 07:01:34",
"updated_at": "2015-04-20 14:04:27",
"orm_bank_contact": [

"id": 27,
"bank_department": 2,
"mobile": "9843139168",
"phone": "9843139168",
"email": "shresthabeenu@gmail.com",
"contact_person": "Binu Shrestha",
"address": "No. 416, Mahabandoola Road, Kyauktada Township, Yangon, Myanmar",
"is_active": "1",
"created_at": "2015-04-15 08:50:16",
"updated_at": "2015-04-15 08:50:16",
"bank_id": 13

]

]

我需要在急切加载的对象上获取带有子项的记录

我在下面尝试过

$bank_contact = BankDepartment::with(array('OrmBankContact' => function($query) use($bank_id)
            $query->where('bank_id', "=", $bank_id)->where('bank_department','>',0);))

            ->get();

但没有运气..什么是正确的方法?

【问题讨论】:

【参考方案1】:

    模拟银行部门

    class BankDepartment extends Eloquent implements UserInterface, RemindableInterface 
    
     public function OrmBankContact()
        return $this->hasMany('OrmBankContact','bank_department','id');
     
    

    模型 OrmBankContact

    class OrmBankContact extends Eloquent implements UserInterface, RemindableInterface 
    
        public function BankDepartment()
    
        return $this->belongsTo('BankDepartment','bank_department','id');
      
    
    
    

在任何控制器中你都可以得到这样的

$OrmBankContact = BankDepartment::with('OrmBankContact')->get();
$OrmBankContact = $OrmBankContact->toArray();
echo '<pre>'; print_r($OrmBankContact); exit;

【讨论】:

【参考方案2】:

这个查询应该只返回带有 OrmBankContact 的部门

BankDepartment::has('OrmBankContact')->get();

See Eloquent docs on querying relations

【讨论】:

以上是关于忽略急切加载的数据的空对象 - laravel的主要内容,如果未能解决你的问题,请参考以下文章

Laravel Eager Loading 删除子项的空值

Laravel with() 急切加载返回空数据

Laravel 自定义数据透视表关系和急切加载?

Laravel 动态关系 - 在急切加载时访问模型属性

Laravel 5.1通过急切加载返回连接字段

Laravel 急切加载特定列错误