任何人都可以解释这种行为吗?十月CMS查询生成器无法获得关系模型
在例子中,两种型号:
人民,国家
人属于关联国家:
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,
},
],
}
我看到人们#1与国家#1的关系,但是当我试图让查询生成器这个关系,空车返回集合:
>>> Test\TestPlugin\Models\People::country()->get();
=> October\Rain\Database\Collection {#970
all: [],
}
>>>
为什么?
我看看为方法,而不是动态属性。我的错误是我尝试将方法应用于许多条目。我必须得到一个条目,然后调用方法,如下所示: Test \ TestPlugin \ Models \ People :: find(1) - > country() - > get(); – user2599458