php - 尝试在 null 上读取属性“doc”
问题描述
我有一个candidat
, 和document
Models 但是当我使用 $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
}
提前致谢
解决方案
推荐阅读
- javascript - 我似乎无法在 javascript/typescript 的数组中更新对象字段的值?
- java - Eclipse Gradle 项目:JAXB 不会从嵌套的 WSDL 文件创建 java 类
- python - Python - 排序列表
- c++ - 如何在 C++ 中分配 const 成员变量之前验证它的初始化
- curl - 'curl localhost:3000/block/1' 出现错误'curl: (56) Recv failure: Connection reset by peer'
- javascript - 如何在从数组扩展的类上制作工作过滤器功能
- javascript - 如何在单击图例时突出显示 chart.js 堆积条形图中的条形
- hardware - 2018 年的 Web 开发需要多少 RAM?
- android - 如何在 android 中通过改造接收来自 laravel 的字符串响应?
- c++ - Arduino C++中的字典类数据结构