简体   繁体   English

分离数据透视表中的行-Laravel

[英]Detaching Rows in Pivot Table - Laravel

I have a pivot table country_team with data in this form 我有一个数据透视表country_team ,数据格式如下

country_team 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. 现在,我想将皇马从行中分离出来,但是如何获得皇马的ID或获得Real Madrid的名字来分离它。 I cannot delete using country_id since it will delete all teams belonging to that particular country . 我无法使用country_id进行删除,因为它将删除该特定国家/地区的所有团队。

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 的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. ()中的选择将返回“皇家马德里”团队2的ID。因此,如果team_id == 2,它将删除country_team中的条目。

Well You can simply pass country_id and team_id and update your delete team as such. 好吧,您只需传递country_id和team_id即可更新您的删除小组。

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. 我个人使用这样的想法来分离ID。
Controller 控制者

public function deleteTeam($country_id, $team_id)
{
    $country = Country::findOrFail($country_id);  
    $country->teams()->detach($team_id);
}

HTML 的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. web.php中,您必须将路由添加到正确的组中。

Route::post('delete/{country_id}/{team_id}', [
    'uses' => 'ControllerName@deleteTeam',
    'as' => 'admin.team.delete'
]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM