【发布时间】:2018-01-14 14:29:19
【问题描述】:
我在使用 Model 对象的 ->with() 方法检索对象时遇到问题。
在我的应用程序中,我有 4 个表格(我在这里写下我认为是表格的基本字段,所以):
**Categories**:
id
**Subcategorie**
id
id_categorie
**UserSubcategorie**
id_user
id_subcategorie
**Lawyers**
user_id
我正在尝试获取给定类别的所有用户(这意味着所有用户都连接到所有类别的子类别)
我的要求如下:
$categorie = LegalCategory::whereIn('id', $arrayCat)->with('legal_subcategories')->get();
foreach ($categorie as $k =>$cat){
$selectedCat[$k]['categorie'] = $cat;
$selectedCat[$k]['lawyers'] =
Lawyer::Join('lawyers_legal_subcategories', 'lawyers.user_id', '=', 'lawyers_legal_subcategories.user_id')
->with('lawyer_subcategories')
->whereIn('lawyers_legal_subcategories.legal_subcategory_id', $cat->legal_subcategories)
->get();
}
我得到了正确的律师,但是当我查看 lawyer->legal_subcategories 时,我有 NULL
我不明白为什么,因为当我尝试这个时:
Lawyer::orderBy('created_at')->take(4)->with('lawyer_subcategories')->get()
我在 Lawyer 对象中有我的子类别。
有人知道问题可能是什么吗?
【问题讨论】:
标签: php laravel model eloquent inner-join