【发布时间】:2017-07-28 14:31:16
【问题描述】:
谁能解释这种行为?
例如,两个模型:
人,国家
人民属于国家:
public $belongsTo = [
'country' => [
'Test\TestPlugin\Models\Country',
]
我为每个模型创建了一个条目,并将其关联起来。
有修补程序转储:
>>> Test\TestPlugin\Models\People::all();
=> October\Rain\Database\Collection {#926
all: [
Test\Testplugin\Models\People {#928
id: 1,
country_id: 1,
},
Test\Testplugin\Models\People {#930
id: 2,
country_id: 0,
},
],
}
>>> Test\TestPlugin\Models\People::with('country')->get();
=> October\Rain\Database\Collection {#963
all: [
Test\Testplugin\Models\People {#943
id: 1,
country_id: 1,
country: Test\Testplugin\Models\Country {#965
id: 1,
name: "Russia",
},
},
Test\Testplugin\Models\People {#945
id: 2,
country_id: 0,
country: null,
},
],
}
我看到 People#1 与 Country#1 有关系,但是当我尝试在查询构建器中获取这种关系时,它返回空集合:
>>> Test\TestPlugin\Models\People::country()->get();
=> October\Rain\Database\Collection {#970
all: [],
}
>>>
为什么?
【问题讨论】:
标签: eloquent query-builder relation octobercms laravel-query-builder