首页 > 解决方案 > 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:

output of echo $output

Now if i use the return view on the controller it displays

Output of the return view:

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>

标签: laravel

解决方案


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

推荐阅读