1
내 laravel 응용 프로그램의 에서 File
테이블을 사용하여 인쇄 사용자 이름이 필요합니다. 이것은 User
모델과 File
모델 사이의 관계입니다.Laravel 5.2의 blade.php에서 ErrorException을 수정하는 방법
파일 모델
public function user()
{
return $this->belongsTo(User::class);
}
이 블레이드 파일
@if($files)
@foreach($files as $file)
<div>
<div>
<span>
{{ $file->user->name }} //line 16
</span>
</div>
</div>
@endforeach
@endif
컨트롤러
class PfilePDFController extends Controller
{
public function getPFPDF($id){
$files = File::where('project_id',$id)->get();
$pdf = PDF::loadView('pdf.projectfiles',['files'=>$files]);
return $pdf->stream('projectfiles.pdf');
}
}
에게 있습니다하지만이 오류가있어 :
ErrorException in bd52d760518dfb36a05daedee198eefcb7b87914.php line 16: Trying to get property of non-object (View: C:\Users\dnk\Desktop\acxian\resources\views\pdf\projectfiles.blade.php)
을
이 문제를 해결하는 방법?
이 문제에 대해 아무런 생각이 없습니다. – DNK
은'$ files'을 보내는 컨트롤러 코드를 공유합니까? – C2486
아마도'user_id'는 null이거나 잘못된 사용자'id'가 있고'$ file-> user'는'null'을 반환합니다. 그래서, 당신은'null'에서'name'을 얻으려고합니다. – Laerte