Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
261 views
in Technique[技术] by (71.8m points)

twitter bootstrap - PHP/Laravel/Bootsrap fetch data

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 | value | count

15 0 10 12 15 1 10 51 ... 15 8 10 12

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).

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>

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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');
    }

}

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>

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;
});

Ask if something is not clear.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...