Php 获取Laravel 5.1中的嵌套关联记录 我有三个数据库表。 型号
存在Php 获取Laravel 5.1中的嵌套关联记录 我有三个数据库表。 型号,php,laravel-5,laravel-5.1,Php,Laravel 5,Laravel 5.1,存在CurrencyID关系和ProjectTypeID关系 我在尝试什么? 在我的Laravel 5.1代码中,我试图实现下面的sql语句,以便可以为每个ProjectType记录获取projecttypecurrencyprice记录。最后,它还应该为每个项目类型currencyPrice记录显示货币表中的记录 $ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model ::with("p
CurrencyID
关系和ProjectTypeID
关系
我在尝试什么?
在我的Laravel 5.1代码中,我试图实现下面的sql语句,以便可以为每个ProjectType记录
获取projecttypecurrencyprice记录。最后,它还应该为每个项目类型currencyPrice
记录显示货币表中的记录
$ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model
::with("projecttypecurrencyprice")
->with("projecttypecurrencyprice.Currency")
->get();
有什么问题吗?
我无法获取每个项目类型CurrencyPrice记录的货币记录。对于项目类型CurrencyPrice\u模型
您应通过以下方式更改货币关系:
public function Currency()
{
return $this->belongsTo('\App\Models\Currency_Model', "CurrencyID");
}
因为您在tblprojecttypecurrencyprice
中有CurrencyId
列,并且您与tblcurrency
中的ProjectTypeCurrencyPrice\u Model
没有任何连接。
并获取数据,而不是:
$ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model
::with("projecttypecurrencyprice")
->with("projecttypecurrencyprice.Currency")
->get();
您可以用
省略第一个:
$ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model
::with("projecttypecurrencyprice.Currency")
->get();
顺便说一下,如果你开始这个项目,你应该考虑数据库结构的变化,并在PHP中读取PSR——你不应该使用<代码> ProjyType CurrrCyryPrice模型> /Case>作为你的模型名称,如果你真的不需要,MySQL < /P>中的<代码> ProjyType ID > /Cuth>列可以粘贴你的模型吗?您定义与其他提到的模型的关系的那部分您是否遇到了某种错误?
$ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model
::with("projecttypecurrencyprice")
->with("projecttypecurrencyprice.Currency")
->get();
$ProjectTypes = \App\Models\project\ProjectType\ProjectType_Model
::with("projecttypecurrencyprice.Currency")
->get();