TiagoF TiagoF - 27 days ago 15
MySQL Question

Laravel Count total entries and make a number list

im trying to make a sort for a project i'm doing, and i want to get the total of entries of my database and then make a select with the total of entries but ordered by 1 to 25 lets say..

My controller looks like this:

$users = User::orderBY('sort', 'DESC')->paginate(10);
return view('admin.user.index', compact('users'));


My view looks like this:

<select class="form-control" id="sort" name="sort">
@foreach($users as $user)
<option value="0">0</option>
@endforeach
</select>


So.. lets say i have 25 users i want 25 option from 1 to 25 in the value, since i cant use the item id.. cuz they can be deleted..

Thanks for the help!

Answer

You can try as:

<select class="form-control" id="sort" name="sort">
    @foreach($users as $key => $user)
        <option value="{{ $key + 1 }}">
            {{ $key + 1 }}
        </option>
    @endforeach
</select>