twitter bootstrap - PHP/Laravel/Bootsrap fetch data -
i've got tiny little problem. i've got table in database called 'player_skills'. contains columns (example):
player_id | skill_id | value | count
the player_id id of player column 'id' under 'players' table. there 8 skills. value default value (which irrelevant in case). count value (the value of players skill).
basically, want pull data 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 girl, section c.</p> </div> </div> </div>
i want order (from highest lowest,( have 'group_id' column in every player, don't want include player has group_id equals three) every skill). have 1 skill that's located in 'players' table (column called 'experience') , i've done this:
public function highscores() { $players = player::orderby('experience','desc')->get(); return view::make('aac.highscores')->with('players', $players); }
it works fine, need in tabs change every skill.
your need covered laravel. first, have declare many many relationship between skills , players (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 can skills database players linked with
method. method not obligatory (you can use all
instead) eager loading 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 can iterate on skills array , on players arrays inside each skill (read more blade templating : http://laravel.com/docs/templates#blade-templating) :
<!-- index.blade.php --> <div class="tabbable tabs-left"> <ul class="nav nav-tabs"> @foreach ($skills $skill) <li><a href="?{{ $skill->id }}" data-toggle="tab">{{ $skill->name }}</a></li> @endforeach </ul> <div class="tab-content"> @foreach ($skills $skill) <div class="tab-pane active" id="?{{ $skill->id }}"> @foreach ($skills->players $player) <p>{{ $player->name }} : {{ $player->pivot->count }} points !</p> @endforeach </div> @endforeach </div> </div>
if want order players skill count can use sortby
function (read more : http://laravel.com/docs/eloquent#collections) :
$orderedplayers = $skill->players->sortby(function($player) { return $player->pivot->count; });
ask if not clear.
Comments
Post a Comment