2 回答
![?](http://img1.sycdn.imooc.com/545863aa00014aa802200220-100-100.jpg)
TA贡献1830条经验 获得超9个赞
用户模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\Skill;
class User extends Model
{
/**
* The skills that belong to the user.
*/
public function skills()
{
return $this->belongsToMany(Skill::class, 'candidate_skills', 'user_id', 'skill_id' )->withPivot('years_xp', 'comfort_level');
}
}
和技能模型
<?php
namespace App;
use Illuminate\Database\Eloquent\Model;
use App\User;
class Skill extends Model
{
/**
* The users that belong to the user.
*/
public function users()
{
return $this->belongsToMany(User::class, 'candidate_skills', 'user_id', 'skill_id' )->withPivot('years_xp', 'comfort_level');
}
}
现在,让用户使用department_id和work_authorization过滤器
$candidates = User::whereIn('department_id', $departmentFilters)
->whereIn('work_authorization', $workAuthFilters)
->orderBy('updated_at', 'desc')
->with('skills')
->get();
并在刀片中显示时
@foreach($candidates as $candidate)
@foreach ( $candidate->skills as $skill )
{{ $skill->name }}
@endforeach
@endforeach
或者
@foreach($candidates as $candidate)
@foreach ( $candidate->skills as $skill )
{{ $skill->pivot->years_xp}}
{{ $skill->pivot->comfort_level}}
@endforeach
@endforeach
我没有演示数据可以对其进行测试,但是我想它可以正常工作。
- 2 回答
- 0 关注
- 223 浏览
添加回答
举报