![](/img/trans.png)
[英]Trying to get property of non-object Laravel 5.3, profile.blade.php.
[英]Trying to get property 'profile_picture' of non-object backend\students\contact.blade.php
我收到此錯誤
Trying to get property 'profile_picture' of non-object (View: D:\projet7899\laravel-only-school-managemnt\resources\views\backend\students\contact.blade.php`)
在contact.blade.php
<img class="w-20 h-20 sm:w-32 sm:h-32 rounded" src="{{ asset('images/profile/' .$student->user->profile_picture) }}" alt="avatar">
在StudentController.php我有這個功能
public function contact(Student $student) {
$class = Grade::with('subjects')->where('id', $student->class_id)->first();
return view('backend.students.contact', compact('class','student'));
}
它與名為 show 的其他函數的結構相同,該函數運行正常,但這個函數運行不佳我只需要獲取用戶電子郵件的值我不知道共享整個 GitHub 鏈接是否合法代碼 。 提前致謝
出現問題是因為當用戶模型實際上不存在時,您試圖訪問user
模型上的profile_picture
。
您需要做的是在您的視圖中檢查以確保用戶模型確實存在
<img class="w-20 h-20 sm:w-32 sm:h-32 rounded" src="{{ !empty($student->user) ? asset('images/profile/' .$student->user->profile_picture) : asset('link to a generic image') }}" alt="avatar">
從學生對象訪問 profile_picture
<img class="w-20 h-20 sm:w-32 sm:h-32 rounded"
src="{{ asset('images/profile/' .$student->profile_picture) }}" alt="avatar">
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.