【问题标题】:Trying to get property of non-object laravel 5 hasOne eloquent试图获得非对象 laravel 5 hasOne eloquent 的属性
【发布时间】:2015-08-14 11:21:49
【问题描述】:

我正在尝试使用 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-&gt;session-&gt;libellesess}}

【问题讨论】:

    标签: eloquent laravel-5 blade php-5.6


    【解决方案1】:

    我已经解决了这个问题:

     @if($exam->session)
         {{$exam->session->libellesess}}
      @endif
    

    【讨论】:

      猜你喜欢
      • 2014-05-05
      • 1970-01-01
      • 2015-12-04
      • 2015-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多