laravel - How to display the array element to view
问题描述
I have a problem regarding the return view('doctor_index', compact('result') on my contoller
here is my controller
public function index()
{
$data = Auth::user()->patient;
$data = explode(',', $data);
foreach ($data as $key => $datas) {
$result = DB::table('patients')->where('id', $datas)->get();
foreach ($result as $key => $res) {
$output = ' <h4><b>'. $res->patient_name .'</b></h4>
</p>Birthday: <strong>'. $res->post_date .'</strong> Age: <strong>'. $res->patients_age .'</strong></p>
<p>Address: <strong>'. $res->patient_address .'</strong></p><br><br>';
}
echo $output;
// return view('doctor_index', compact('output'));
}
}
at first i used echo $output; this is what it displayed
Output of echo $output:
Now if i use the return view on the controller it displays
Output of the return view:
as you can see when i use the return view it only display the first element
My question is how can i display all of the elements to my view using the return view
my view code:
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-12">
<h2><b>{{ Auth::user()->name }} </b></h2>
<p>Email: <strong> {{ Auth::user()->email }} </strong></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-12">
<h3>Patients</h3>
</div>
<div class="col-md-12">
<div class="container">
<?php echo $output ?>
</div>
</div>
</div>
</div>
</div>
解决方案
使用函数和方法,一旦到达 return 语句,它就不会继续通过其他循环。相反,您应该将数组传递给视图并在视图中循环遍历数组。所以你的控制器看起来像这样:
public function index()
{
$data = Auth::user()->patient;
$data = explode(',', $data);
$responseData = [];
foreach ($data as $key => $datas) {
$result = DB::table('patients')->where('id', $datas)->get();
foreach ($result as $key => $res) {
$responseData[] = ' <h4><b>'. $res->patient_name .'</b></h4>
</p>Birthday: <strong>'. $res->post_date .'</strong> Age: <strong>'. $res->patients_age .'</strong></p>
<p>Address: <strong>'. $res->patient_address .'</strong></p><br><br>';
}
}
return view('doctor_index', compact('responseData'))
}
然后你的刀片模板看起来像这样。
<div class="col-md-8 col-md-offset-2">
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-12">
<h2><b>{{ Auth::user()->name }} </b></h2>
<p>Email: <strong> {{ Auth::user()->email }} </strong></p>
</div>
</div>
</div>
<div class="panel panel-default">
<div class="panel-body">
<div class="col-md-12">
<h3>Patients</h3>
</div>
<div class="col-md-12">
<div class="container">
@foreach($responseData as $output)
{!! $output !!}
@endforeach
</div>
</div>
</div>
</div>
</div>
推荐阅读
- r - R中的Highcharts:在折线图中对xAxis进行排序
- java - 回收站视图使用按钮从图库或相机添加图像
- scala - Protobuf com.esotericsoftware.kryo.KryoException: java.lang.UnsupportedOperationException unknownFields
- postgresql - postgresql:如何添加一个列,该列自动存储带有时间戳的行的创建时间
- twitter-bootstrap - 防止表元素脱离 Bootstrap 列和手风琴部分
- java - 方法reduce(Task, BinaryOperator
) 在流类型中 不适用于参数(HashMap > - time - 从 Kivy MDTimePicker 中抽出时间
- postgresql - cursor.copy_from 不工作但没有错误
- javascript - 未正确解析 CSV 数据?
- java - build-impl.xml:919:模块尚未部署