Answer the question
In order to leave comments, you need to log in
How to assign a number to a user by rating and display it in the blade?
Hi all.
There is a project on laravel
Here is the database migration
Schema::create('gamers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('nick');
$table->string('time');
$table->timestamps();
});
public function index()
{
return view('game')
->with('gamers', Gamer::all());
}
<table class="table table-striped">
<thead>
<tr>
<th scope="col">Позиция</th>
<th scope="col">Nick</th>
<th scope="col">Время</th>
<th scope="col"></th>
</tr>
</thead>
<tbody>
@foreach ($gamers as $g)
<tr>
<th scope="row">1</th>
<td>{{ $g->nick }}</td>
<td>{{ $g->time }}</td>
<td></td>
</tr>
@endforeach
</tbody>
</table>
@foreach ($gamers->sortBy('time') as $g)
<tr>
<th scope="row">1</th> //// но как менять тут 1,2,3,4 внутри foreach
<td>{{ $g->nick }}</td>
<td>{{ $g->time }}</td>
<td></td>
</tr>
@endforeach
Answer the question
In order to leave comments, you need to log in
First, in the controller, you can immediately get the sorted data:
public function index()
{
return view('game')
->with('gamers', Gamer::orderBy('time', 'desc')->get()); // ну или создать дефолтный отсортированный скоуп, если это всегда требуется
}
@foreach ($gamers as $index=>$g)
<tr>
<th scope="row">{{$index + 1}}</th>
<td>{{ $g->nick }}</td>
<td>{{ $g->time }}</td>
<td></td>
</tr>
@endforeach
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question