0

我尝试使用 hasOne 关系,但出现以下错误:尝试获取非对象的属性(查看:/home/vagrant/Code/gsup_backend/resources/views/exam/index.blade.php) 我的模型

class Session extends Model {

/*
 * @var String
 *
 */
protected $table = 'gs_session';
protected $primaryKey = 'idsess';
public $timestamps = false;
public function exam(){
    return $this->belongsTo('App\Model\Exam','idex','idsess');
}}

考试模式

class Exam extends Model {
/*
* @var String
 */
protected $table = 'gs_exam';
public $timestamps = false;
protected $primaryKey = 'idex';
/*
 * @var String
 */
protected $fillable = ['*'];

public function matiere(){
    return $this->hasOne('App\Model\Matiere','idmat','idex');
}
public function session(){
    return $this->hasOne('App\Model\Session','idsess','idex');
}
public function personne(){
    return $this->hasOne('App\Model\Personne','idper','idex');
}}

我的控制器动作

public function index()
{
    $exams = Exam::all();

    return view('exam.index',compact('exams'));
}

我的观点

  @foreach($exams as $exam)
                        <tr class="gradeX">
                            <td>{{$exam->personne->prenomper}}  {{$exam->personne->nomper}}</td>
                                <td>{{$exam->matiere->libelleapomat}} </td>
                                <td>{{$exam->session->libellesess}}</td>
                            <td class="center">{{$exam->statut}}</td>
                            <td class="center">{{$exam->date}}</td>
                            <td class="center">{{$exam->heuredeb}}</td>
                            <td class="center">{{$exam->heurefin}}</td>
                            <td class="center"><span><button class="btn btn-primary">Modifier</button></span>
                            <span><button class="btn btn-danger">Supprimer</button></span>
                            </td>
                        </tr>
                        @endforeach

我认为问题就在这里{{$exam->session->libellesess}}

4

1 回答 1

2

我已经解决了这个问题:

 @if($exam->session)
     {{$exam->session->libellesess}}
  @endif
于 2015-06-01T05:31:00.807 回答