具有 (id | user_id | name) 列的配置文件模型的用户关系为:
public function user(){
return $this->belongsTo(User::class);
}
用户模型的技能关系为:
public function skills()
{
return $this->belongsToMany(Skill::class);
}
数据透视表是 Skill_user 和 user_id | Skill_id 列与 Skill 模型用户关系为:
public function users(){
return $this->belongsToMany(User::class);
}
如何从搜索请求中搜索具有特定技能的个人资料。
$profiles = Profile::with('users')->where('name', 'like', ''.$term.'%')->get();
但我收到一个错误:not profile_skill table!
我可以通过以下方式接触到具有特定技能的每个个人资料
$profile->user->skills
但我如何才能获得这些技能:
<input name="term" type="text" />
我已经在配置文件模型中测试了用户关系,但出现错误:
public function users(){
return $this->hasManyThrough(User::class, Skill::class, 'skill_id', 'user_id');
}
最佳答案
此外,您可能想尝试 whereHas ,我认为这是最简单的:
return Profile::whereHas('user.skills', function ($query) use ($term) {
$query->where('name', 'like', "{$term}%");
})
->get();
关于laravel - 如何在 Laravel 中从第二个模型到第一个模型获取搜索到的数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63861529/