我试图使用hasOne关系,但是我收到以下错误: 试图获取非对象的属性(查看:/home/vagrant/Code/gsup_backend/resources/views/exam/index.blade.php) 我的模特
SELECT MIN(salary), MAX(salary), AVG(salary), teamID, yearID
FROM salaries
GROUP BY teamID, yearID;
考试模式
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
答案 0 :(得分:2)
我解决了这个问题:
@if($exam->session)
{{$exam->session->libellesess}}
@endif