1

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>
2
  • 1
    It is because you have written the echo inside foreach but when you return it to view it will return only one Commented Oct 11, 2018 at 13:22
  • i tried to echo $output outside the foreach it displayed the last element not all of it Commented Oct 11, 2018 at 13:27

1 Answer 1

1

With functions and methods once it reaches the return statement it won't continue through the other loops. Instead you should pass the array to the view and loop over the array within your view. So your controller would look something like this:

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'))
}

And then your blade template would look something like this.

<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>
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.