我的目标是使用<select name="team">
中的JS函数来过滤<select name="unit">
。团队与单位之间是多对一的关系,因此过滤它会更加安全。我在下面列出了表格和测试代码:
团队单位数据透视表
-------------------
Team --------- Unit
A ------------- 1
B ------------- 1
C ------------- 1
D ------------- 2
E ------------- 2
F ------------- 3
-------------------
<select name="unit" class="form-control" id="unit" onchange="refreshTeam()" required />
<option value="" selected disabled>SELECT UNIT</option>
@foreach ($units as $unit)
<option value="{{$unit->id}}">{{ $unit->title }}</option>
@endforeach
</select>
<select name="team" class="form-control" id="team" required />
<option value="" selected disabled>SELECT TEAM</option>
@foreach ($teams as $team)
<option value="{{$team->id}}">{{ $team->title }}</option>
@endforeach
</select>
这是我对refreshTeam()的尝试,但是我很确定这是错误的或需要改进
function refreshTeam() {
var team = document.getElementById("team");
var unit = document.getElementById("unit");
<?php
$unit = 'document.getElementById("unit").value';
$team_unit = \DB::table('teams')
->leftJoin('team_unit', 'teams.id', '=', 'team_unit.team_id')
->leftJoin('units', 'units.id', '=', 'team_unit.unit_id')
->select('teams.*')
->where($unit, '=', 'team_unit.unit_id')
->get();
dd($team_unit);
?>
}
答案 0 :(得分:0)
refreshTeam函数应调用GET /unit/1/teams
,这将返回与单元1相关的所有团队。
/unit/{unit}/teams
路线