【问题标题】:Detaching Rows in Pivot Table - Laravel在数据透视表中分离行 - Laravel
【发布时间】:2017-11-13 07:50:53
【问题描述】:

我有一个数据透视表country_team,其中包含这种形式的数据

国家队

 country_id    team_id  
     1            1
     1            2

国家

   id    name 
    1    Spain 

团队

 id     name
  1     Barcelona
  2     Real Madrid

在我的表格中,我以这种形式显示它

表格

 Country_id    Team
     1         Barcelona
     1         Real Madrid

现在,我想将皇家马德里从行中分离出来,但是如何获取皇家马德里的 id 或获取名称 Real Madrid 来分离它。我无法使用 country_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();  
}

HTML

<tbody>
    @foreach($countries as $country)
        @foreach($country->teams as $team)
        <tr>
            <td>{{$country->id }}</td>
            <td>{{ $team->name}}</td>
        </tr>
        @endforeach
    @endforeach
</tbody>

【问题讨论】:

    标签: php laravel


    【解决方案1】:

    我个人使用类似这样的方法来分离 id。
    控制器

    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>
    

    并且在您的 web.php 中,您必须将路线添加到正确的组或其他内容中。

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

    【讨论】:

    • 或者使用模型绑定:public function deleteTeam(Country $country, Team $team) {} 并更新路由到Route::delete('delete/{country}/{team}'
    【解决方案2】:

    使用子查询:

    DELETE FROM country_team
    WHERE team_id = (SELECT id FROM Team WHERE name = 'Real Madrid')
    

    () 中的选择将返回球队 'Real Madrid', 2 的 id。因此,如果 team_id == 2,它将删除 country_team 中的条目。

    【讨论】:

    • 我能不能请你用 laravel 写它,代码应该是通用的。不一定是Real Madrid,而是任何其他团队。我希望能够将刀片中的 id 传递给控制器​​。
    • 从我给出的回复中,你只是放了一个变量而不是'皇家马德里',你就是金子。至于使用 Laravel 特定的语法,我无能为力,因为我不使用它。对不起。
    • 好的,我正在尝试让从刀片传递到控制器的 id 到 variable,就像你说的那样。但我坚持使用正确的语法
    • 用你目前拥有的代码制定一个特定于 Laravel 的新问题。 Laravel 大师们肯定会让你失望。
    【解决方案3】:

    您可以简单地传递 country_id 和 team_id 并更新您的删除团队。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-09
      • 2018-04-10
      • 1970-01-01
      • 2020-08-09
      • 2021-10-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多