【问题标题】:i want to display all the player of the respective club and have some issue in table in laravel我想显示各个俱乐部的所有球员,并在 laravel 的表格中有一些问题
【发布时间】:2015-10-19 05:36:14
【问题描述】:

我有一个带有字段的俱乐部表:

id,name,est_date,CEO ....

我有球员表有字段:

id, name,DOB,height,weight, .....

player_histories 表:

id,player_id,previous_club_id,current_club_id,position, ......

club_histories 表:

club_id, championship_id,status, ...

在 club_histories 的查看页面中,我可以显示:

club name,championship name and status...

但我也想在 club_histories 表中显示俱乐部的相应球员......这怎么可能??? 请回复...

编辑部分:添加代码

Routes.php:

Route::resource('clubhistories','ClubHistoryController');

ClubHistoryController.php:

public function show($id,ClubHistory $clubhistories)
{
    $club=$clubhistories->find($id);

    $clubhistories=$clubhistories->getPlayerByCLub();
    return view('pages.singleClub',compact('club','clubhistories'));
}

ClubHistoryRepository.php

  public function getPlayerByCLub(){
    return $this->makeModel() ->join('clubs', 'clubs.id', '=', 'club_histories.club_id')
        ->join('player_histories','player_histories.current_club_id', '=', 'clubs.id')
        ->join('players', 'players.id', '=', 'player_histories.player_id')
        ->get([
            'clubs.name',
            'player_histories.player_id',
            'players.name',
        ]);
}

SingleClub.blade.php:

 <div class="tab-content">
                    <div class="tab-pane in active" id="basic">

                        <table class="table table-condensed">
                            <thead>
                            <tr>
                                <th>Name</th>
                                <th>contact</th>
                            </tr>
                            </thead>
                            <tbody>
                                <tr>
                                  <td>Name:</td>
                                  <td>{!!$club->club->name!!}</td>

                                </tr>
                                  <tr>
                                      <td>Contact:</td>
                                      <td>{!!$club->club->contact_no!!}</td>
                                   </tr>
                            </tbody>
                        </table>
                    </div>
                    <div class="tab-pane fade" id="players">
                        <table class="table table-condensed">
                            <thead>
                            <tr>
                                <th>Player name</th>
                                <th>Position</th> 
                            </tr>
                            </thead>
                            <tbody>

                            @foreach($clubhistories as $clubhistories)
                            <tr>

                                <td> {!! $clubhistories->name!!}   </td>
                                <td>   {!! $clubhistories->position!!} </td>

                            </tr>
                            @endforeach
                            </tbody>
                        </table>
                    </div>

上面使用的加入和您给出的解决方案给出了相同的解决方案..我想将俱乐部对象中的俱乐部历史找到的相同 id 传递给俱乐部历史对象,以便显示俱乐部的所有球员.. 你能帮帮我吗..

【问题讨论】:

  • 如果您愿意,请考虑遵循以下简单的两步操作: 1. 如果您还没有这样做,请提供适当的 DDL(和/或 sqlfiddle),以便我们可以更轻松地复制问题。 2. 如果您尚未这样做,请提供与步骤 1 中提供的信息相对应的所需结果集。

标签: php mysql laravel migration project


【解决方案1】:

您可以通过连接(较少查询)或 Eloquent 关系(看起来更简单的代码)查看 club_histories 中的球员。

使用Eloquent Relationships(假设您已设置它们):

$histories = ClubHistory::with('players', 'club')->get();

// In your blade view
@foreach ($histories as $history)
    <p>{{ $history->club->name }} won {{ $history->championship_name }}</p>
    <p>Status: {{ $history->status }}
    <div>Player list:
        <ul>
            foreach ($history->players as $player)
                <li>{{ $player->name }}</li>
            @endforeach
        </ul>
    </div>
@endforeach

加入查询生成器:

DB::table('club_histories')
    ->join('player_histories',
        'player_histories.current_club_id', '=', 'club_histories.club_id')
    ->join('players', 'players.id', '=', 'player_histories.player_id')
    ->join('club', 'club.id', '=', 'club_histories.club_id')
    ->get([
        'club.name AS clubName',
        'club_histories.championship_name',
        'club_histories.status',
        'player_histories.player_id',
        'players.name AS playerName',
    ]);

请注意,以上内容将为每个俱乐部历史记录中的每位球员返回一行

编辑:

查看您的代码后,下面是上面的一个版本,它应该适用于您的非标准代码库。请务必将 ID 传递给getPlayerByClub($id)

ClubHistoryRepository.php

public function getPlayerByCLub($clubHistoryId) {
    return $this->makeModel()
        ->join('clubs', 'clubs.id', '=', 'club_histories.club_id')
        ->join('player_histories', 'player_histories.current_club_id', '=', 'clubs.id')
        ->join('players', 'players.id', '=', 'player_histories.player_id')
        ->where('club_histories.id', '=', $clubHistoryId)
        ->get([
            'clubs.id',
            'club_histories.championship_name',
            'club_histories.status',
            'player_histories.player_id',
            'clubs.name',
            'players.name',
        ]);
}

【讨论】:

  • 我将编辑代码...上面,我想在单页中显示...我正在使用资源控制器
  • 啊,我看到至少一个问题.. 我忘了在我的答案中添加“AS”子句。您需要club.name AS clubNameplayers.name AS playerName,这样两个“名称”字段就不会发生冲突。然后在您的刀片文件中,您将能够执行 $clubhistories-&gt;clubName$clubhistories-&gt;playerName
  • 它显示了相同的解决方案..它没有获得俱乐部历史 id... 例如:localhost/fcimsProject/clubhistories/1
  • 我没用过这行:$histories = ClubHistory::with('players', 'club')->get();因为它给出了 ->with 的错误
  • 很难用这些有限的信息来帮助你——我不知道ClubHistoryRepository 是什么类型的对象,或者这个makeModel() 方法是什么——它们不是标准的 Laravel。我将尝试使用可行的代码更新我的答案。您正在获取所有记录,因为您没有在查询中使用 $id-&gt;join() 语句。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-26
  • 1970-01-01
  • 1970-01-01
  • 2021-08-05
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多