使用数据透视表Laravel过滤数据 [英] Filter data with pivot table Laravel

查看:45
本文介绍了使用数据透视表Laravel过滤数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个表:

  • 伊格拉奇(英语为演奏者)
  • Turnir(英语锦标赛)
  • NastupTurnir(这两个之间的数据透视表)

我需要创建一个表,其中将显示所有玩家,这将是表中的行,而列将是锦标赛.这次比赛的价值将是玩家在那场比赛中获胜的点.

I need to create table where will show all the players which will be the rows in the table, while columns will be tournaments. Value of this tournaments will be point which did player won on that tournament.

我设法做到了,但是有一个缺陷.在我的桌子上,它还显示了没有任何锦标赛价值的玩家.

I manage to did this but whit one flaw. On my table it shows also players who doesn't have value for any of the tournaments.

如何过滤玩家以使其不在桌子上显示?问题

How can I filter players to not show them in table? Problem

我的代码:

Controller.php

<?php

  namespace App\Http\Controllers\Frontend;

  use App\Http\Controllers\Controller;
  use Illuminate\Http\Request;
  use App\Sezona;
  use App\Igrac;
  use App\TurnirPiramida;

  class RangListaPiramidaController extends Controller
  {
   public function index()
  {
    $sezone = Sezona::orderBy('godina', 'desc')->get();

    //$kola = TurnirPiramida::where('sezona_id', )

    //dd($igraci);
    return view("frontend.ranglista_piramida", compact('sezone'))->render();
}

public function show($id)
{
    $sezone = Sezona::orderBy('godina', 'desc')->get();

    $sezona = Sezona::findOrFail($id);

    $igraci = Igrac::get();

    $kola = TurnirPiramida::where('sezona_id', $id)->get();

    //$ups = Igrac::find(1);

    //dd($ups->igrac_piramida);

    return view("frontend.ranglista_piramida_kola", compact('sezone', 'sezona', 'igraci', 'kola'))->render();
}
}

刀片文件

@extends('layouts.frontend')
@section('title', 'TK Pazin | Rang-liste Piramida')
@section('content')
<!-- Page Content -->
<div class="container">
<!-- Page Heading -->
<h1 class="my-4" style="text-align:center; color: #ba3631;">Rang-lista | Piramida {{ $sezona->godina }}</h1>
<nav aria-label="breadcrumb">
    <ol class="breadcrumb">
    @foreach($sezone as $sezona)
        <li class="breadcrumb-item"><a href="{{ route('ranglista.piramida.kola', $sezona->id) }}">{{ $sezona->godina }}</a></li>
    @endforeach
    </ol>
</nav>
<div>
    <div class="media mb-5">
        <div class="mr-3"><a href="../datoteke/Pravila-Pojedinačni-turniri.pdf" target="_blank"><i
            class="far fa-file-pdf fa-3x"></i></a></div>
        <div class="media-body mt-3">
            <a href="/datoteke/Pravila-Piramida.pdf" target="_blank">
            <h5 class="mt-0">Pravila
                natjecanja u piramidi
            </h5>
            </a>
        </div>
    </div>
</div>

<div class="table-responsive">
    <table class="table table-hover">
        <thead class="thead-dark">
            <tr>
            <th scope="col">#</th>
            <th scope="col">Ime i prezime</th>
            @foreach($kola as $kolo)
                <th scope="col">{{ $kolo->naziv }}</th>
            @endforeach
            <th scope="col">Ukupno</th>
            </tr>
        </thead>
        <tbody>
            @foreach($igraci as $igrac)
            <tr>
                <th scope="row">1</th>
                <td>
                    <button type="button" class="btn btn-outline-primary" data-toggle="modal" data-target="#view_{{ $igrac->id }}">{{ $igrac->ime . " " . $igrac->prezime }}</button>
                    <!-- Modal -->
                    <div class="modal fade" id="view_{{ $igrac->id }}" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
                        <div class="modal-dialog" role="document">
                            <div class="modal-content">
                                <div class="modal-header">
                                <h5 class="modal-title" id="exampleModalLabel">Natjecatelj: {{ $igrac->ime . " " . $igrac->prezime }}</h5>
                                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                                <span aria-hidden="true">&times;</span>
                                </button>
                                </div>
                                <div class="modal-body">
                                <div class="row">
                                    <div class="col-6">
                                        <img src="/img/avatar.png" alt="..." class="img-thumbnail">
                                    </div>
                                    <div class="col-6">
                                        <b>Prebivalište: </b>
                                        <p> {{ $igrac->prebivaliste }}</p>
                                        <b>Igra: </b>
                                        <p> {{ $igrac->igra }}</p>
                                        <b>Član kluba od:</b>
                                        <p> {{ $igrac->clanstvo }}. godine</p>
                                    </div>
                                </div>
                                <hr>
                                <div class="row">
                                    <div class="col-12 col-md-5 card card-body m-2">
                                        <div class="row">
                                            <div class="col-8">
                                            <b>Najbolji ranking piramida: </b>
                                            </div>
                                            <div class="col-4">
                                            <span class="badge badge-success p-3">1.</span>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-12 col-md-6 card card-body m-2">
                                        <div class="row">
                                            <div class="col-8">
                                            <b>Najbolji ranking pojedinačni turniri: </b>
                                            </div>
                                            <div class="col-4">
                                            <span class="badge badge-success p-3">5.</span>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                </div>
                                <div class="modal-footer">
                                <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
                                </div>
                            </div>
                        </div>
                    </div>
                </td>
                    @foreach($kola as $kolo)
                        @foreach($igrac->igrac_piramida as $ip)
                            @if($ip->pivot->turnir_piramida_id == $kolo->id)
                                <td>{{ $ip->pivot->bodovi }}</td>
                            @endif
                        @endforeach
                    @endforeach

            </tr>
            @endforeach
        </tbody>
    </table>
</div>
</div>
<!-- /.container -->
@endsection

推荐答案

如果我理解正确,则希望获取曾经参加过锦标赛的玩家.

If I understand properly, you want to fetch players that have been in a tournament.

您当前正在获取所有玩家:

You are currently fetching all player:

$igraci = Igrac::get();

您应该取回仅在Tournament_player数据透视表中找到的玩家.

You should instead fetch only the players found in the tournament_player pivot table.

从数据透视表中获取不同的玩家ID:

Get distinct player ids from the pivot table:

$playerIds = DB::table('nastup_turnir')->groupBy('igrac_id')->pluck('igrac_id');

从其ID中获取玩家:

Igrac::whereIn('id', $playerIds)->get();


注意:您的数据透视表不遵循Laravel命名约定.如果数据透视表包含 igrac_id turnir_id 列,则应将其命名为 igrac_turnir .


Note: Your pivot table doesn't follow Laravel naming convention. If you pivot table contains igrac_id and turnir_id columns, it should be named igrac_turnir.

这篇关于使用数据透视表Laravel过滤数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆