How best way to display the sequence number?

Published 2 months ago by wardaddy

I want to display the serial number of each row of data on two tables, and I want the number to start from one on each table, like the table below

DJ List

| # | id | username | etc |

|---|----|-------------|------|

| 1 | 1 | john | ... |

| 2 | 2 | jane | ... |

User List

| # | id | username | etc |

|---|----|-------------|------|

| 1 | 3 | dave | ... |

| 2 | 4 | dick | ... |

but what I get is like in the table below

DJ List

| # | id | username | etc |

|---|----|-------------|------|

| 1 | 1 | john | ... |

| 2 | 2 | jane | ... |

User List

| # | id | username | etc |

|---|----|-------------|------|

| 3 | 3 | dave | ... |

| 4 | 4 | dick | ... |

here my code

manageUsers.blade.php

<div class="col s12">
        <h5 class="light">DJ List</h5>
    <table class="responsive-table">
        <thead>
            <tr>
                <th>#</th>
                <th>ID</th>
                <th>Username</th>
                <th>Email</th>
                <th>Channel</th>
                <th>Changed on</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($djs as $dj)
                <tr>
                    <td>{{ $no++ }}</td>
                    <td>{{ $dj->id }}</td>
                    <td>{{ $dj->name }}</td>
                    <td>{{ $dj->email }}</td>
                    <td>{{ $dj->channel_name }}</td>
                    <td>{{ $dj->updated_at }}</td>
                    <td>
                        <a href="/users/{{ $dj->id }}/delete">delete</a>
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
    <h5 class="light">User List</h5>
    <table class="responsive-table">
        <thead>
            <tr>
                <th>#</th>
                <th>ID</th>
                <th>Username</th>
                <th>Email</th>
                <th>Joined on</th>
                <th>Action</th>
            </tr>
        </thead>
        <tbody>
            @foreach ($users as $user)
                <tr>
                    <td>{{ $no++ }}</td>
                    <td>{{ $user->id }}</td>
                    <td>{{ $user->name }}</td>
                    <td>{{ $user->email }}</td>
                    <td>{{ $user->created_at }}</td>
                    <td>
                        <a href="/user/{{ $user->id }}/delete">delete</a>
                    </td>
                </tr>
            @endforeach
        </tbody>
    </table>
</div>

UserController.php

public function index()
{
    $djs = User::join('channels', 'users.id', '=', 'channels.user_id')
                    ->select('users.*', 'channels.name as channel_name')
                    ->where('type_id', '=', '2')
                    ->orderBy('updated_at', 'desc')
                    ->get();
    $users = User::where('type_id', '=', '3')->orderBy('created_at', 'desc')->get();

    return view('dashboard.manageUsers', compact('users', 'djs'))->with('no', 1);
}

can someone tell where my fault is, or help to tell a better way than this?

Best Answer (As Selected By wardaddy)
bobbybouwmann

Aah sorry I can't read!

I want to display the serial number of each row of data on two tables, and I want the number to start from one on each table, like the table below

You can simply use the loop functionality that is build into blade for this

@foreach ($djs as $dj)
    // Do your thing and echo the count here
    <td>{{ $loop->iteration }}</td>
@endforeach 

@foreach ($users as $user)
    // Do your thing and echo the number here
    <td>{{ $loop->iteration }}</td>
@endforeach

Note that $loop->iteration is handled by Laravel in your blade files. The $loop variable contains more data if you need that as well.

See here for the documentation: https://laravel.com/docs/5.5/blade#the-loop-variable

Let me know if this is what you want! Otherwise I need some more information

bobbybouwmann

You can simply use a counter for this using PHP

<?php $number = 0; ?>

@foreach ($djs as $dj)
    // Do your thing and echo the number here
    <td>{{ $number }}</td>

    <?php $number++; ?>
@endforeach 

@foreach ($users as $user)
    // Do your thing and echo the number here
    <td>{{ $number }}</td>

    <?php $number++; ?>
@endforeach
wardaddy

@bobbybouwmann it just like I did before, but I initialize the variable number on the controller ->with('no', 1);

here

return view('dashboard.manageUsers', compact('users', 'djs'))->with('no', 1);

and here

<td>{{ $no++ }}</td>
bobbybouwmann

Aah sorry I can't read!

I want to display the serial number of each row of data on two tables, and I want the number to start from one on each table, like the table below

You can simply use the loop functionality that is build into blade for this

@foreach ($djs as $dj)
    // Do your thing and echo the count here
    <td>{{ $loop->iteration }}</td>
@endforeach 

@foreach ($users as $user)
    // Do your thing and echo the number here
    <td>{{ $loop->iteration }}</td>
@endforeach

Note that $loop->iteration is handled by Laravel in your blade files. The $loop variable contains more data if you need that as well.

See here for the documentation: https://laravel.com/docs/5.5/blade#the-loop-variable

Let me know if this is what you want! Otherwise I need some more information

Snapey
Snapey
2 months ago (707,405 XP)

before your second use of $no reset it back again... (php!)

Cronix
Cronix
2 months ago (175,200 XP)

Personally I'd use the $loop variable supplied by laravel automatically, as mentioned by @bobbybouwmann (less code, don't have to pass $no from controller), but as @snapey is saying, all you'd have to do is reset $no back to 1 before you start iterating the $users:

@php $no = 1; @endphp
@foreach ($users as $user)
     // rest as you have 
wardaddy

@bobbybouwmann thanks, it's very easy to use because I do not need to write a variable on the controller to get it

Please sign in or create an account to participate in this conversation.