TiagoF TiagoF - 1 year ago 70
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>

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 Source

You can try as:

<select class="form-control" id="sort" name="sort">
    @foreach($users as $key => $user)
        <option value="{{ $key + 1 }}">
            {{ $key + 1 }}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download