TiagoF TiagoF - 23 days ago 8
MySQL Question

Count total entries and make a number list in Laravel

I'm 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 let's 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>


Let's say I have 25 users. I want 25 options from 1 to 25 in the value, since I can't use the item id.. because they can be deleted.

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>