我有三个求职者,工作和技能。一个用户有很多技能,而且一份工作有很多技能,所以我有这些关系,
class Job extends Model
{
public function skills()
{
return $this->belongsToMany(Skill::class, 'jobs_skills', 'job_id', 'skill_id');
}
}
和
class Seeker extends Model
{
public function skills()
{
return $this->belongsToMany(Skill::class, 'seekers_skills', 'seeker_id', 'skill_id');
}
}
和
class skills extends Model
{
public function jobs()
{
return $this->belongsToMany(Job::class, 'jobs_skills', 'skill_id', 'job_id')->withPivot('level1');
}
public function seekers()
{
return $this->belongsToMany(Seeker::class, 'seekers_skills', 'skill_id', 'seeker_id');
}
}
我写了这个查询,根据技能
向求职者提供相关工作public function relatedJobs()
{
$skills = $this->belongsToMany(Skill::class, 'seekers_skills', 'seeker_id', 'skill_id')->pluck('skill_id');
$job_ids = DB::table('jobs_skills')->whereIn('skill_id', $skills)->pluck('job_id');
$jobs = Job::whereIn('id', $job_ids)->with('skills')->get()->sortBy(function ($job) {
return $job->skills2->groupBy('pivot.job_id')->count();
});
return $jobs;
}
此功能返回相关作业,但未按最佳匹配搜索者技能排序 任何帮助? 顺便说一下,我需要返回作为Job的对象