Php 分离数据透视表中的行-Laravel
我有一个数据透视表Php 分离数据透视表中的行-Laravel,php,laravel,Php,Laravel,我有一个数据透视表country\u team,数据格式如下 国家队 country_id team_id 1 1 1 2 id name 1 Barcelona 2 Real Madrid 国家 id name 1 Spain 团队 country_id team_id 1 1 1
country\u team
,数据格式如下
国家队
country_id team_id
1 1
1 2
id name
1 Barcelona
2 Real Madrid
国家
id name
1 Spain
团队
country_id team_id
1 1
1 2
id name
1 Barcelona
2 Real Madrid
在我的表格中,我将它显示在以下表格中
表格
Country_id Team
1 Barcelona
1 Real Madrid
现在,我想把皇家马德里从这一行中分离出来,但如何才能获得皇家马德里的id或名称皇家马德里
来分离它。我无法使用国家/地区id删除,因为它将删除属于该特定国家/地区的所有团队
控制器
public function index()
{
$countries = Country::where('id',Auth::user()->id)->get();
return view('admin.order.index',compact('orders'));
}
public function deleteTeam()
{
$get_country_id = Country::findOrFail($id);
$get_country_id->teams()->detach();
}
public function deleteTeam($country_id, $team_id)
{
$country = Country::findOrFail($country_id);
$country->teams()->detach($team_id);
}
HTML
<tbody>
@foreach($countries as $country)
@foreach($country->teams as $team)
<tr>
<td>{{$country->id }}</td>
<td>{{ $team->name}}</td>
</tr>
@endforeach
@endforeach
</tbody>
<tbody>
@foreach($countries as $country)
@foreach($country->teams as $team)
<tr>
<td>{{ $country->id }}</td>
<td>{{ $team->name }}</td>
<td>
<form action="{{ route('admin.team.delete', ['country_id' => $country->id, 'team_id' => $tem->id]) }}" method="post">
<button type="submit" role="button">Delete</button>
{{ csrf_field() }}
</form>
</td>
</tr>
@endforeach
@endforeach
</tbody>
@foreach($国家作为$国家)
@foreach($country->teams as$team)
{{$country->id}
{{$team->name}
@endforeach
@endforeach
使用子查询:
DELETE FROM country_team
WHERE team_id = (SELECT id FROM Team WHERE name = 'Real Madrid')
select in()将返回“皇家马德里”2队的id。因此,如果团队id=2,它将删除国家团队中的条目。使用子查询:
DELETE FROM country_team
WHERE team_id = (SELECT id FROM Team WHERE name = 'Real Madrid')
select in()将返回“皇家马德里”2队的id。因此,如果team_id==2,它将删除country_team中的条目。您只需传递country_id和team_id并更新您的delete team即可
public function deleteTeam(Scountry_id,$team_id)
{
$country = Country::findOrFail($country_id);
$country->teams()->detach($team_id);
}
您只需传递国家id和团队id并更新您的删除团队即可
public function deleteTeam(Scountry_id,$team_id)
{
$country = Country::findOrFail($country_id);
$country->teams()->detach($team_id);
}
我个人使用类似这样的方法来分离ID。
控制器
public function index()
{
$countries = Country::where('id',Auth::user()->id)->get();
return view('admin.order.index',compact('orders'));
}
public function deleteTeam()
{
$get_country_id = Country::findOrFail($id);
$get_country_id->teams()->detach();
}
public function deleteTeam($country_id, $team_id)
{
$country = Country::findOrFail($country_id);
$country->teams()->detach($team_id);
}
HTML
<tbody>
@foreach($countries as $country)
@foreach($country->teams as $team)
<tr>
<td>{{$country->id }}</td>
<td>{{ $team->name}}</td>
</tr>
@endforeach
@endforeach
</tbody>
<tbody>
@foreach($countries as $country)
@foreach($country->teams as $team)
<tr>
<td>{{ $country->id }}</td>
<td>{{ $team->name }}</td>
<td>
<form action="{{ route('admin.team.delete', ['country_id' => $country->id, 'team_id' => $tem->id]) }}" method="post">
<button type="submit" role="button">Delete</button>
{{ csrf_field() }}
</form>
</td>
</tr>
@endforeach
@endforeach
</tbody>
我个人使用类似这样的方法来分离ID。
控制器
public function index()
{
$countries = Country::where('id',Auth::user()->id)->get();
return view('admin.order.index',compact('orders'));
}
public function deleteTeam()
{
$get_country_id = Country::findOrFail($id);
$get_country_id->teams()->detach();
}
public function deleteTeam($country_id, $team_id)
{
$country = Country::findOrFail($country_id);
$country->teams()->detach($team_id);
}
HTML
<tbody>
@foreach($countries as $country)
@foreach($country->teams as $team)
<tr>
<td>{{$country->id }}</td>
<td>{{ $team->name}}</td>
</tr>
@endforeach
@endforeach
</tbody>
<tbody>
@foreach($countries as $country)
@foreach($country->teams as $team)
<tr>
<td>{{ $country->id }}</td>
<td>{{ $team->name }}</td>
<td>
<form action="{{ route('admin.team.delete', ['country_id' => $country->id, 'team_id' => $tem->id]) }}" method="post">
<button type="submit" role="button">Delete</button>
{{ csrf_field() }}
</form>
</td>
</tr>
@endforeach
@endforeach
</tbody>
我可以请你写在拉威尔和代码应该是通用的。不一定是皇家马德里,而是任何其他球队。我希望能够将刀片服务器的id传递给控制器。根据我给出的响应,你只需输入一个变量,而不是“皇家马德里”,你就是黄金。至于使用特定于Laravel的语法,我无能为力,因为我不使用它。抱歉。好的,我正在尝试将刀片服务器传递给控制器的id放入一个
变量中,就像您所说的那样。但我一直坚持正确的语法,用你到现在为止的代码,为Laravel提出了一个新的问题。拉威尔大师肯定会解开你的枷锁。我能不能请你用拉威尔来写,代码应该是通用的。不一定是皇家马德里
,而是任何其他球队。我希望能够将刀片服务器的id传递给控制器。根据我给出的响应,你只需输入一个变量,而不是“皇家马德里”,你就是黄金。至于使用特定于Laravel的语法,我无能为力,因为我不使用它。抱歉。好的,我正在尝试将刀片服务器传递给控制器的id放入一个变量中,就像您所说的那样。但我一直坚持正确的语法,用你到现在为止的代码,为Laravel提出了一个新的问题。Laravel gurus肯定会解开你的枷锁。或者使用模型绑定:公共函数deleteTeam(Country$Country,Team$Team){}
并将路由更新到route::delete('delete/{Country}/{Team}
或者使用模型绑定:公共函数deleteTeam(Country$Country,Team$Team){}
并将路由更新为route::delete('delete/{country}/{team}'