【发布时间】:2017-11-21 07:26:45
【问题描述】:
我在 Laravel eloquent 中获取 hasmany 关系数据时遇到问题。
AModel 有许多 BModel AModel 有很多 CModel
AModel 具有功能:
Public function bmodels(){
return hasMany('BModel');
}
Public function cmodels(){
return hasMany('CModel');
}
BModel 有功能:
Public function bmodels(){
return belongsTo('AModel', amodel_id);
}
CModel 有功能:
Public function cmodels(){
return belongsTo('AModel',amodel_id);
}
现在我正在尝试像这样得到它
$amodels = AModel::with('bmodels','cmodels')
->where('status_id','2200')
->whereIn('eventstatus_id',['1','2'])
->get();
现在我想在 for 循环中测试它。
foreach($amodels as $amodel){
$bmodels = $model->bmodels();
if ($amodel && $amodel->end < Carbon::now()){
foreach ($bmodels as $bmodel){
$cmodels = $amodel->cmodels();
foreach($cmodels as $cmodel){
if ($bmodel->id !== $cmodel->reservation_id || $cmodel->reservation_id != null ){
array_push($reservation_needed_to_enter,$bmodel->id);
}
}
}
}
}
【问题讨论】:
-
要访问这些模型,只需在 foreach 循环中编写 $amodel->bmodels 而不是 $amodel->bmodels()。
-
感谢现在的作品
-
你能接受它作为答案吗?然后我会发布一个答案
-
我怎么能接受我看不到任何答案
-
好的,我正在发布它..
标签: php mysql laravel orm eloquent