如何显示要查看的数组元素

时间:2018-10-11 13:17:45

标签: laravel

关于我的contoller上的返回视图(“ doctor_index”,compact(“ result”))有问题

这是我的控制器

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'));
    }
}
首先,我使用echo $ output;这就是它显示的内容

回显$ output的输出:

output of echo $output

现在,如果我在控制器上使用返回视图,则会显示

返回视图的输出:

output of the return view

如您所见,当我使用返回视图时,它仅显示第一个元素

我的问题是如何使用返回视图将所有元素显示到视图中

我的查看代码:

<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>

1 个答案:

答案 0 :(得分:1)

有了函数和方法,一旦到达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>