我想展示一家公司的所有职位。我已经定义了关系模型。
class Company extends Model
{
//
protected $fillable = [
'user_id',
'name',
'company_size',
'slogan',
'website',
'logo',
'message_title',
'message_content',
'main_photo',
'about_us',
'why_us',
'recruiting_steps',
'address',
'email',
'phone_number',
'location',
];
public function Users(){
return $this->hasMany('App\Models\User');
}
public function job_posts(){
return $this->hasMany('App\Models\JobPost', 'company_id');
}
}
职位模型:
class JobPost extends Model
{
protected $table = 'job_posts';
protected $fillable = [
'company_id',
'user_id',
'title',
'summary',
'description',
'requirements',
'benefits',
'approval',
'location',
'publish_date',
'expiration_date'
];
public function user()
{
return $this->belongsTo('App\Models\User');
}
public function company()
{
return $this->belongsTo('App\Models\Company', 'company_id');
}
public function cv_folders()
{
return $this->hasMany('App\Models\CvFolder');
}
}
我的公司控制器显示方法
public function show(Company $company)
{
//
$company = Company::find($company->id);
$JobPosts = $company->JobPosts;
return view('Companies.show', [
'company' => $company,
'JobPosts' => $JobPosts,
]);
}
我的公司认为:
@foreach($JobPosts as $jobPost)
<div class="col-md-12">
<div class="box box-default box-solid">
<div class="box-header with-border">
<h3 class="box-title">{{$jobPost->title}}</h3>
<div class="box-tools pull-right">
<button type="button" class="btn btn-box-tool" data-widget="collapse"><i
class="fa fa-minus"></i>
</button>
</div>
<!-- /.box-tools -->
</div>
<!-- /.box-header -->
<div class="box-body" style="">
The body of the box
</div>
<!-- /.box-body -->
</div>
<!-- /.box -->
</div>
@endforeach
但我收到了错误
“为foreach()提供的参数无效(查看:C:\ xampp \ htdocs \ hrlead \ resources \ views \ companies \ show.blade.php)” 请帮我
答案 0 :(得分:1)
重命名您的方法:
public function job_posts(){
为:
public function jobPosts(){
并重命名
$JobPosts = $company->JobPosts;
为:
$JobPosts = $company->jobPosts;
在此之后它应该有效。
答案 1 :(得分:0)
您正在调用错误的变量。
它应该是SliverAppBar
而不是$company->job_posts
。 PHP在变量和函数中有点区分大小写。