I have a pivot table country_team
with data in this form
country_team
country_id team_id
1 1
1 2
Country
id name
1 Spain
Team
id name
1 Barcelona
2 Real Madrid
In my table, i have it displayed in this form
Table
Country_id Team
1 Barcelona
1 Real Madrid
Now, i want to detach real Madrid from the row but how can get the id of real madrid or get the name Real Madrid
to detach it. I cannot delete using country_id since it will delete all teams belonging to that particular country .
Controller
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();
}
HTML
<tbody>
@foreach($countries as $country)
@foreach($country->teams as $team)
<tr>
<td>{{$country->id }}</td>
<td>{{ $team->name}}</td>
</tr>
@endforeach
@endforeach
</tbody>
Use a sub-query:
DELETE FROM country_team
WHERE team_id = (SELECT id FROM Team WHERE name = 'Real Madrid')
The select in () will return the id for team 'Real Madrid', 2. So it will delete entries in country_team if team_id == 2.
Well You can simply pass country_id and team_id and update your delete team as such.
public function deleteTeam(Scountry_id,$team_id)
{
$country = Country::findOrFail($country_id);
$country->teams()->detach($team_id);
}
I personally use somethink like this to detach ids.
Controller
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>
<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>
And in your web.php you have to add the route in the correct group or something.
Route::post('delete/{country_id}/{team_id}', [
'uses' => 'ControllerName@deleteTeam',
'as' => 'admin.team.delete'
]);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.