【发布时间】:2018-05-20 02:19:24
【问题描述】:
我在下面有三个表格: 我想在单个职位发布中按类别显示所有相关职位发布。我已经有一个职位发布页面,但在同一个职位页面中,我想在左侧显示相关职位。看我的照片!
控制器应该是什么,单作业页面(视图)应该是什么?请帮忙?
我的工作控制器
public function show($id, $company_id)
{
$singleJob = Job::find($id);
$company = Company::find($company_id);
$similarJob = Job::with('company')->where('category_id', $id)->get();
return view('pages.single-job')->with([
'singleJob'=> $singleJob,
'company'=> $company,
'similarJob' => $similarJob,
]);
}
我的关系
job.php
public function company(){
return $this->belongsTo(Company::class);
}
Job.php
public function category(){
return $this->belongsTo(Category::class);
}
//category.php
public function job(){
return $this->hasMany(Job::class);
}
//company.php
public function job(){
return $this->hasMany(Job::class);
}
工作表
Schema::create('jobs', function (Blueprint $table) {
$table->increments('id');
$table->integer('company_id');
$table->string('jobTitle');
$table->longText('jobDescription');
公司表
Schema::create('company_types', function (Blueprint $table) {
$table->increments('id');
$table->integer('admin_id');
$table->string('name');
$table->timestamps();
});
分类表
Schema::create('categories', function (Blueprint $table) {
$table->increments('id');
$table->integer('user_id');
$table->string('name');
$table->timestamps();
});
【问题讨论】:
-
您是否忘记了 Job 迁移中的
category_id否则它将不起作用?那么外键约束呢? -
是的,我忘记包含了,我会编辑