我有两张桌子,"帐户"和"后续"。我希望能够通过FollowupController提取数据以传递给视图并运行foreach循环。
例如;我在帐户表中有多行帐户,只有某些行会通过外键链接到它。 我希望能够遍历这些后续行动。从后续表中提取数据很容易。但我不确定如何从同一个foreach循环中的accounts表中提取数据。
这是我到目前为止(显然不起作用);
查看:
@foreach($accounts as $account)
{{$account->sName}}
@foreach($followups as $followup)
<div style="padding-bottom: 5px">
<h5>{{$followup->followSubject}}</h5>
<p><b>Date Created: </b> {{$followup->followCreateDate}}</p>
<p><b>Follow Up By: </b> {{$followup->followUpEndDate}}</p>
<p>{{$followup->followNotes}}</p>
</div>
@endforeach
@endforeach
控制器:
public function index()
{
$followups = Followup::all();
$accounts = Account::all();
return view('followups')->with('followups', $followups)
->with('accounts', Account::all());
}
帐户模型:
function followups() {
return $this->hasMany(Followups::class);
}
跟进模式:
public function account() {
return $this->belongsTo('App\Account');
}
提前致谢。
答案 0 :(得分:1)
只需加载它们,即可显示有后续跟踪的内容:
Data$shift[grepl("3rd", Data$shift)] <- "Third Shift"
Data$shift[grepl("1st", Data$shift)] <- "First Shift"
然后在刀片上访问它们:
public function index()
{
$accounts = Account::with('followups')->get();
return view('followups', ['accounts'=>$accounts]);
}
更多检查Laravel eageloading