Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124

How to increment letters like numbers in Laravel

Increment letters like numbers in Laravel

In this tutorial, I will give you an example of the “How to Increment letters like numbers in Laravel“, So you can easily apply it with your laravel 5, laravel 6, laravel 7, and laravel 8 application.

First, what we’re doing here, This is the example :

Increment letters like numbers in laravel

Need of list Alphabetical (A-Z) in Laravel

Whenever we work on invoice or inventory applications in Laravel, Sometimes we need to show the sub-items or subcategories under the alphabet for better alignment of invoices.

In this example, We will get all data from table data from the database and show the sequence alphabetically instead of numbers in the blade file.

Database Table :

We have a test table in the database and we already stored some dummy data in this table using faker.

We created a model Test Table1, using this model we got all the data from the table.

Best way to list Alphabetical (A-Z) using PHP

routes\web.php

 Route::get('/increment-letters','TestController@index');

App\Http\Controllers\TestController

 <?php

 namespace App\Http\Controllers;

 use Illuminate\Http\Request;
 use App\Model\TestTable1;

 class TestController extends Controller
 {

  #example of increment letter
  public function index(){
    $data = TestTable1::all();
    return view('incrementexample',compact('data'));
  }
 }

resources\views\incrementexample.blade.php

Now, you can see the output in the blade file, we have successfully implemented the list Alphabetical (a-z) using PHP in the Laravel application.

    <div class="container">
         <h3>Increment letters like numbers (a - z) in Laravel</h3>
         <table class="table">
            <thead>
               <tr>
                  <th>S.no</th>
                  <th>Name</th>
                  <th>Email</th>
                  <th>Address</th>
               </tr>
            </thead>
            <tbody>

               @php 

               $char=1;
               $albha = 'a'; $char < 'z';

               @endphp

               @foreach($data as $data)
               <tr>
                  <td><b>{{ $albha++ }}.</b></td>
                  <td>{{$data->name}}</td>
                  <td>{{$data->email}}</td>
                  <td>{{$data->address}}</td>
               </tr>
            </tbody>
            @endforeach
         </table>
    </div>
Increment letters like numbers in laravel

If you want to capitalize the alphabet like (A-Z), just capitalize the alphabet string in code.

  @php

  $char=1;
  $albha = 'A'; $char < 'Z';

  @endphp
Best way to list Alphabetical (A-Z) using PHP

In this article, we learned “Increment letters like numbers in Laravel”, I hope this article will help you with your Laravel application Project.

Also Read : Create a custom helper function in Laravel.