簡體   English   中英

試圖獲取非對象后端\\students\\contact.blade.php 的屬性“profile_picture”

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM