$data['customers'] = DB::table('master_customer')->where('ORG_ID', $user_org)->get();//customer table
$document_cat = DB::table('document_category')->get();//category table
foreach ($data['customers'] as $key => $value) {
foreach ($document_cat as $key1 => $value1) {
$docs = DB::table('master_documents')//docuements saved in this table
->where('cust_id', $value->id)
->get();
$document_cat[$key1]->doc = $docs;
}
$data['customers'][$key]->documents = $document_cat;
}
print_r($data);
die;
为什么不更新模型中的雄辩关系?如果你这样做,它可能会容易得多。
主客户模型
public function masterDocuments()
{
return $this->hasMany('App\correctfoldertoothermodel\MasterDocument','cust_id');
}
主文档模型
public function masterCustomer()
{
return $this->belongsTo('App\correctfoldertoothermodel\MasterCustomer','cust_id');
}
public function documentCategory()
{
return $this->belongsTo('App\correctfoldertoothermodel\DocumentCategory');
}
文档类别模型
public function masterDocuments()
{
return $this->hasMany('App\correctfoldertoothermodel\MasterDocument');
}
当然,您必须在迁移中提供正确的表名,以便自动实现 fk 或在关系中设置表。
完成后,您可以像这样检索数据:
$masterCustomers->with('masterDocuments.documentCategory')->get();
$masterCustomers->masterDocuments = $masterCustomers->masterDocuments->groupBy(function($item){
return $item->documentCategory->name;
});
结果将是具有和属性的客户集合,其中包含另一个类别集合及其文档。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句