![](/img/trans.png)
[英]LARAVEL - how to retrieve data from three tables connected through two pivot tables
[英]How to get data from 3 tables connected with a pivot in php laravel
我遇到了一個問題,我無法通過 pivot 表檢索數據,
在我的應用程序中有一個單獨的表來存儲文件數據和一個單獨的表來存儲比賽和一個單獨的表來存儲團隊。
還有一個名為 Competition_file 的表,其中包含 Competition_id、 competition_file
competition_id, team_id, file_id
。
用戶可以通過選擇一個團隊或不選擇一個團隊來將多個文件添加到比賽中,如下圖所示
它將被同步到competition_file
表,如下圖所示
我需要將 UI 中的所有數據顯示為下圖所示的表格
我想知道如何填寫使用 pivot 表選擇的team name
File Model
中的關系
public function teams()
{
return $this->belongsToMany(Team::class,'competition_file')->wherePivot('type','document');
}
Competition Model
public function documents()
{
return $this->belongsToMany(File::class,'competition_file','competition_id', 'file_id')->wherePivot('type','document');
}
這是我的controller index
function
public function index($id)
{
$competition = $this->competitionsRepo->find($id,[
'team',
'documents',
]);
return view('competitions.document',[
'competition'=>$competition,
]);
}
我的刀片
@foreach ($competition->documents as $key=>$document)
<tr>
<td>{{ $key+1 }}</td>
<td>{{ $document->name }}</td>
<td>-</td>
<td>{{ $document->created_at->toDateString() }}</td>
<td>
<form class="confirm" action="{{ route('documents.destroy',['competition'=>$competition,'document'=> $document->id]) }}" method="POST"
data-confirm-title="Remove Document?"
data-confirm-message="Are you sure you want to remove this document?">
@csrf
@method('DELETE')
<button type="submit" name="button" class="button">Remove</button>
</form>
</td>
</tr>
@endforeach
有人請幫我在刀片中將團隊名稱添加到我的 foreach 中
我希望我的問題很清楚,如果沒有問題請評論編輯問題
我能夠通過推薦學習渴望加載來解決我的問題
這是我所做的更改
在controller
public function index($id)
{
$competition = $this->competitionsRepo->findOrFail($id);
$files = $competition->documents()->with(
[
'teams' => function($q) use ($competition) {
$q->with([
'documents' => function($q) use($competition) {
$q->where([
'competition_id' => $competition->id,
]);
}
]);
},
]
)->get();
return view('competitions.document',[
'id'=>$id,
'competition'=>$competition,
'files'=>$files,
]);
}
在我的Blade
表中,我使用$file->teams->implode('name')
<table class="table light-td sports-tbl tb-chg2 table-condensed table-borderless">
<thead>
<tr>
<th>#</th>
<th>Document Name</th>
<th>Assigned Team</th>
<th>Date Added</th>
<th>Action</th>
</tr>
</thead>
<tbody>
@foreach ($files as $key=>$file)
<tr>
<td>{{ $key+1 }}</td>
<td>
<a href="{{ asset('storage/'. $file->file_path) }}" class="text-info" target="_blank">
<img src="{{ asset('/images/sketches/1x/file.png') }}" alt="file">
</a>
{{ $file->name }}
</td>
<td>
{!! $file->teams->isNotEmpty() ? $file->teams->implode('name') : '<span class="text-secondary">No teams selected</span>' !!}
</td>
<td>{{ $file->created_at->toDateString() }}</td>
<td>
<form class="confirm" action="{{ route('documents.destroy',['competition'=>$id,'document'=> $file->id]) }}" method="POST"
data-confirm-title="Remove Document?"
data-confirm-message="Are you sure you want to remove this document?">
@csrf
@method('DELETE')
<button type="submit" name="button" class="button">Remove</button>
</form>
</td>
</tr>
@endforeach
</tbody>
</table>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.