首页 > 解决方案 > 尝试在 null 上读取属性“doc”

问题描述

我有一个candidat, 和documentModels 但是当我使用 $candidat->document->doc 时,我会尝试在 null 上读取属性“doc”

Document Model

 class Document extends Model
{
use HasFactory;
protected $table = 'documents';
protected $fillable = [
    'id_candidat',
    'doc',
    'date_depot'];

public function candidat()
{
    return $this->belongsTo(candidat::class, 'candidat_id');
}
}

Candidat Model

  class candidat extends Model
{
use HasFactory;
protected $table = 'candidats';
protected $fillable = [
    'nom',
    'prenom',
    'email',
    'adresse',
    'date_naissance',
    'telephone',
    'cin',
    ];

  public function documents()
{
    return $this->hasMany(document::class);
}

CandidatController

  public function show()
   {
    $candidat = candidat::all();
    return view('candidat.liste', compact('candidat'));

}

candidat/liste

  @foreach($candidat as $key => $data)
 {....
  {{$data->document->doc}} #this provoked the error
 }

提前致谢

标签: phplaravellaravel-5eloquent

解决方案


推荐阅读