我创建了一个视图文件,并且有多个模型。我要在视图中显示多个模型值。
我想使用“学生模型”显示学生详细信息,并使用“中心模型”显示show center_code。
我尝试过,但显示出一些错误。
Property [center_code] does not exist on this collection instance. (View: C:\resources\views\Center\registretion_form_hardcopy.blade.ph
一流学生
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
class Student extends Model
{
protected $table = 'student_lists';
protected $fillable = ['student_name', 'student_registration_id', 'date_of_join', 'student_phone_no',];
}
第二名模范生
<?php
namespace App;
use Illuminate\Notifications\Notifiable;
use Illuminate\Contracts\Auth\MustVerifyEmail;
use Illuminate\Foundation\Auth\User as Authenticatable;
use Illuminate\Database\Eloquent\Model;
class Center extends Model
{
protected $table = 'centers';
protected $fillable = ['center_code','center_name'];
}
}
路线
Route::get('registretion_form_hardcopy{id}', 'StudentController@registretion_form_hardcopy');
控制器
public function registretion_form_hardcopy($id)
{
$hardcopy = Student::where('delete_status','NOT DELETED')->where('center_accepting_online_application','ACCEPTED')->where('center_approved','APPROVED')->where('center_code','=',Auth::user()->center_code)->find($id);
$center_details_hardcopy = Center::where('center_code','=',Auth::user()->center_code)->get();
$pdf = PDF::loadView('Center.registretion_form_hardcopy', compact('hardcopy','center_details_hardcopy'))->setPaper('a4', 'portrait');
$fileName = $hardcopy->student_registration_id;
return $pdf->stream($fileName . '.pdf');
}
查看
<!DOCTYPE html>
<html>
<head>
<!-- <title>0</title> -->
<style>
*{margin: 0; padding: 0;}
body{ font-size:20px; }
.banner{position: relative; width: 90%; margin: 0 auto; }
.banner img{width: 100%}
.heading{color: red; position: absolute; top: 50%; width: 100%; text-align: center; font-size:3rem;text-shadow: 5px 5px 10px #000000; }
.x{color: pink; position: absolute; top: 50%; width: 100%; text-align: }
@page {
size: 8.5in 11in;
margin: .5in;
}
#bgimg {
position: fixed;
left: .0in;
top: -.1in;
width: 8.5in;
height: 11in;
z-index: -999
}
p {
padding-left: 100px;
font-style: initial;
}
b {
padding-left: 100px;
font-style: initial;
}
#header{
position: relative;
}
#logo{
position: absolute;
right: 30px;
}
.left{
float:left;
}
.right{
float:right;
}
div.r {
line-height: 90%;
}
</style>
</head>
<body>
<div class="container">
<div class="banner">
<img id="bgimg" src="center student hardcopy.jpg" alt="img" >
</div>
<font size="-1"><p style="font-family:sans-serif; "><b4 style="font-family:sans-serif;padding-left:170px;">{{$hardcopy->student_registration_id}}</b4></p></font>
<br>
<font size="-1"><p style="font-family:sans-serif; "><b4 style="font-family:sans-serif;padding-left:170px;">{{$hardcopy->student_name}}</b4></p></font>
<br>
<br>
<br>
<font size="-1">
<div class="r"><b3 style="font-family:sans-serif;padding-left:525px; text-align:right">{{$center_details_hardcopy->center_code}}<div></div></b3></font>
</div><br>
</body>
</html>
感谢任何帮助。
答案 0 :(得分:1)
您将在控制器中获得多个集合,因此请使用first()
而不是get()
控制器
中的更改$center_details_hardcopy = Center::where('center_code','=',Auth::user()->center_code)->first();
更改刀片文件
{{$center_details_hardcopy->center_code ?? ''}}