PHP/Laravel/Bootsrap获取数据 [英] PHP/Laravel/Bootsrap fetch data

查看:44
本文介绍了PHP/Laravel/Bootsrap获取数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小问题.我在数据库中有一个名为"player_skills"的表.它包含这样的列(示例):

I've got an tiny little problem. I've got a table in database called 'player_skills'. It contains columns like this (example):

player_id | skill_id |值|计数

player_id | skill_id | value | count

  • 15 0 10 12
  • 15 1 10 51
  • ...
  • 15 8 10 12
  • player_id实际上是玩家的ID,即玩家"表下的列"id". 另外还有八种技能.值是默认值(在这种情况下无关紧要).计数就是价值(球员技能的价值).

    The player_id is actually an id of the player which is column 'id' under 'players' table. Also there are eight skills. Value is the default value (which is irrelevant in this case). The count is the value (the value of the players skill).

    基本上,我想要将数据拉入Bootstrap选项卡(示例):

    Basically, what I want is to pull data into a Bootstrap tabs (example):

            <div class="tabbable tabs-left">
        <ul class="nav nav-tabs">
        <li class="active"><a href="?skill0" data-toggle="tab">Section 1</a></li>
        <li><a href="?skill1" data-toggle="tab">Section 2</a></li>
        <li><a href="?skill2" data-toggle="tab">Section 3</a></li>
    ...
        </ul>
        <div class="tab-content">
        <div class="tab-pane active" id="?skill0">
        <p>I'm in Section A.</p>
        </div>
        <div class="tab-pane" id="?skill1">
        <p>Howdy, I'm in Section B.</p>
        </div>
        <div class="tab-pane" id="?skill2">
        <p>What up girl, this is Section C.</p>
        </div>
        </div>
        </div>
    

    我想对它进行排序(从最高到最低,(而且我在每个玩家中都有一个"group_id"列,因此我不希望它包括一个group_id等于3的玩家),但对于每项技能).另外,我在玩家"表(称为体验"列)中有一项技能,我是这样完成的:

    I want to order it (from the highest to the lowest,( also I have an 'group_id' column in every player, so I don't want it to include a player which has an group_id equals to three) but for every skill). Also I have one skill that's located in the 'players' table (column called 'experience') and I've done it like this:

        public function highscores()
    {
        $players = Player::orderBy('experience','desc')->get();
        return View::make('aac.highscores')->with('players', $players);
    }
    

    它工作得很好,但是我需要在选项卡中针对每种技能进行更改.

    It works just fine, but I need it in tabs to change for every skill.

    推荐答案

    Laravel确实满足了您的需求.首先,您必须像这样声明技能和玩家之间的多对多关系(了解更多: http://laravel.com/docs/eloquent#many-to-many ):

    Your need is really well covered by Laravel. First, you have to declare a many to many relationship between skills and players like this (read more : http://laravel.com/docs/eloquent#many-to-many) :

    class Skill extends Eloquent {
    
        public function players() {
            return $this->belongsToMany('Player', 'player_skills', 'skill_id', 'player_id')
                        ->withPivot('value', 'count');
        }
    
    }
    

    然后,您可以通过与with方法链接的球员从数据库中获取所有技能.此方法不是强制性的(您可以使用all代替),但是在此处进行快速加载是一种好习惯(更多信息:

    Then you can get all your skills from your database with the players linked with the with method. This method is not obligatory (you can use all instead) but eager loading is a good practice here (read more : http://laravel.com/docs/eloquent#eager-loading) :

    class SkillController extends BaseController {
    
        public function index()
        {
            $skills = Skill::with('players')->get();
    
            return View::make('skill.index', array('skills' => $skills));
        }
    
    }
    

    最后,可以遍历您的技能数组和每种技能内的玩家数组(了解有关刀片模板的更多信息:

    Finally just can iterate over your skills array and over the players arrays inside each skill (read more about blade templating : http://laravel.com/docs/templates#blade-templating) :

    <!-- index.blade.php -->
    <div class="tabbable tabs-left">
        <ul class="nav nav-tabs">
        @foreach ($skills as $skill)
            <li><a href="?{{ $skill->id }}" data-toggle="tab">{{ $skill->name }}</a></li>       
        @endforeach
        </ul>
        <div class="tab-content">
        @foreach ($skills as $skill)
            <div class="tab-pane active" id="?{{ $skill->id }}">
                @foreach ($skills->players as $player)
                    <p>{{ $player->name }} : {{ $player->pivot->count }} points !</p>
                @endforeach
            </div>
        @endforeach
        </div>
    </div>
    

    如果您想按技能计数来订购球员,可以使用类似的sortBy功能(更多信息:

    If you want to order your players by skill count you can use the sortBy function like that (read more : http://laravel.com/docs/eloquent#collections) :

    $orderedPlayers = $skill->players->sortBy(function($player) {
        return $player->pivot->count;
    });
    

    询问不清楚的地方.

    这篇关于PHP/Laravel/Bootsrap获取数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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