undefined variable在laravel中加载视图时

时间:2017-07-25 03:56:23

标签: php laravel-5

我在View中收到了未定义变量的错误,即使我已从doctorAppointment模型中的表中检索数据,然后将其传递给Controller,然后控制器将其进一步传递给查看。 这是模型

<?php

   namespace App;

   use Illuminate\Database\Eloquent\Model;

   class doctorAppointment extends Model
   {
      public function user()
      {
          return $this->belongsTo('App\User');
      }
   }

这是我的控制器

class AppointmentController extends Controller

{
      public function GetAppointment()
      {
        $doctorAppointment= doctorAppointment::all();
        return view('dashboard',array('appointments'=>$doctorAppointment));
      }
  }

这是我的观点

@foreach($appointments as $appointment)
  <p>{$appointment->patientEmail}</p>
@endforeach

这是我的路线

Route::get('/dashboard',  'AppointmentController@GetAppointment');

3 个答案:

答案 0 :(得分:0)

像这样使用

socket

答案 1 :(得分:0)

您认为<p>代码值不应该在双花括号{{ }}内。

<p>{{ $appointment->patientEmail }}</p>

参考:https://laravel.com/docs/5.4/blade#displaying-data

对于未定义的案例,请尝试:

<p>{{ $appointment->patientEmail or 'No Email'}}</p>

答案 2 :(得分:0)

尝试此代码可以帮助

class AppointmentController extends Controller
{
  public function GetAppointment()
  {
    $appointments= doctorAppointment::all();
    return view('dashboard',compact('appointments'));
  }
}