Laravel 5.8:试图获取非对象的 属性 'created_at' 时发生奇怪的错误
Laravel 5.8: Weird Error Trying to get property 'created_at' of non-object
我正在使用 Laravel 5.8 开发我的项目,在这个项目中,我在 Member
模型和 [=17= 之间有一个 OneToMany 关系] 像这样的模型:
Student.php
:
public function member()
{
return $this->belongsTo(Student::class, 'std_mbr_id');
}
Member.php
:
public function student()
{
return $this->hasOne(Student::class,'std_mbr_id');
}
然后在控制器上,我添加了这个:
public function index()
{
$student = new Student();
$students = $student->searchStudents()->paginate(20);
$custom = new Student();
$customs = $custom->all();
return view('admin.students.custom', compact('students','customs'));
}
现在在视图中我想获得 created_at
时间戳,如下所示:
$customs->find($student->mbr_id)->created_at
但这会return这个错误:
正在尝试获取 属性 'created_at' 的非对象
然而,当我 dd($customs->find($student->mbr_id))
时,我可以正确获取时间戳:
那么在这种情况下如何访问 created_at
时间戳?
更新#1:
{{ dd($customs->find($student->mbr_id)->created_at ) }}
的结果:
首先检查是否 $customs->find($student->mbr_id)
returns 东西:
$customs->find($student->mbr_id) ? $customs->find($student->mbr_id)->created_at : 'not found';
我正在使用 Laravel 5.8 开发我的项目,在这个项目中,我在 Member
模型和 [=17= 之间有一个 OneToMany 关系] 像这样的模型:
Student.php
:
public function member()
{
return $this->belongsTo(Student::class, 'std_mbr_id');
}
Member.php
:
public function student()
{
return $this->hasOne(Student::class,'std_mbr_id');
}
然后在控制器上,我添加了这个:
public function index()
{
$student = new Student();
$students = $student->searchStudents()->paginate(20);
$custom = new Student();
$customs = $custom->all();
return view('admin.students.custom', compact('students','customs'));
}
现在在视图中我想获得 created_at
时间戳,如下所示:
$customs->find($student->mbr_id)->created_at
但这会return这个错误:
正在尝试获取 属性 'created_at' 的非对象
然而,当我 dd($customs->find($student->mbr_id))
时,我可以正确获取时间戳:
那么在这种情况下如何访问 created_at
时间戳?
更新#1:
{{ dd($customs->find($student->mbr_id)->created_at ) }}
的结果:
首先检查是否 $customs->find($student->mbr_id)
returns 东西:
$customs->find($student->mbr_id) ? $customs->find($student->mbr_id)->created_at : 'not found';