dousuochu7291
2017-11-13 07:50
浏览 29
已采纳

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

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>

图片转代码服务由CSDN问答提供 功能建议

我有一个数据透视表 country_team </ code>,其中包含此格式的数据</ p>

country_team </ strong> </ p>

  country_id team_id 
 1 1 
 1 2 
 </ code> </ pre> 
 \  n 

国家</ strong> </ p>

  id name 
 1 Spain 
 </ code> </ pre> 
 
 

< strong>团队</ strong> </ p>

  id name 
 1 Barcelona 
 2 Real Madrid 
 </ code> </ pre> 
 
 

In 我的表格,我把它以这种形式显示</ p>

表</ strong> </ p>

  Country_id Team 
 1 Barcelona \  n 1皇家马德里
 </ code> </ pre> 
 
 

现在,我想从排名中分离出真正的马德里,但如何获得真正的马德里的身份或获得名称皇家马德里 </ code>分离它。 我无法使用country_id删除,因为它会删除属于该特定国家/地区的所有团队。</ p>

控制器</ strong> </ p>

   public function index()
 {
 $ countries = Country :: where('id',Auth :: user() - &gt; id) - &gt; get(); 
 
返回视图('admin。  order.index',compact('orders')); 
} 
 
公共函数deleteTeam()
 {
 $ get_country_id = Country :: findOrFail($ id);  
 $ get_country_id-&gt; teams() - &gt; detach();  
} 
 </ code> </ pre> 
 
 

HTML </ strong> </ p>

 &lt; tbody&gt; 
 @foreach  ($ country as $ country)
 @foreach($ country-&gt; teams as $ team)
&lt; tr&gt; 
&lt; td&gt; {{$ country-&gt; id}}&lt; / td&gt; \  n&lt; td&gt; {{$ team-&gt; name}}&lt; / td&gt; 
&lt; / tr&gt; 
 @endforeach 
 @endforeach 
&lt; / tbody&gt; 
 </ code> </ pre  > 
 </ div>

3条回答 默认 最新

相关推荐 更多相似问题